red-e
9/19/2017 - 4:24 AM

Loading missing images from live on staging via htaccess rewrite rule

A strategy was described in this presentation to display missing images on a staging site without having to sync the wp-content/uploads folders. https://stevegrunwell.github.io/wordpress-git/#/13, https://stevegrunwell.com/blog/keeping-wordpress-under-version-control-with-git/

Notes:

  • All of the code above # BEGIN WordPress should be added to the top of the staging site’s root .htaccess file
  • Make sure to replace the URL in the rewrite rule (https://yoursite.com) with the correct URL for the live site.
  • The IfModule code block for loading files from /wp-content on the live site must come BEFORE WordPress’ own htaccess directive
  • If you’re going to have the RewriteRule all on one line, remove the \ and just use a space between the first and second parts. The \ is to indicate that there should be no line break.
<IfModule mod_rewrite.c>
  RewriteEngine on

  # Attempt to load files from production if
  # they're not in our local version
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteRule wp-content/uploads/(.*) \
    https://yoursite.com/wp-content/uploads/$1 [NC,L]
</IfModule>

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On

RewriteBase /profdevstaging/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /profdevstaging/index.php [L]

</IfModule>

# END WordPress