Multisite: Develop locally using production images with .htaccess

The following .htaccess to rewrite example …

RewriteCond %{REQUEST_URI} ^/wp-content/uploads/[^\/]*/.*$
RewriteRule ^(.*)$ http://example.com/$1 [QSA,L]

Just to note… this will result in an external redirect (as if R=302 was included on the RewriteRule directive), not simply a “rewrite” (as is suggested) – even without the R flag. So, the browser will issue two requests for every resource in the uploads directory. This might be OK whilst developing in a local test environment (as you are doing), but you’d never do something like this in production, as it could literally make your site grind to a halt if you have many external resources. (To do this properly, you would need to configure your server as a reverse proxy and use mod_proxy to “proxy” the request.)

If you specify an absolute URL (ie. with scheme + hostname) in the RewriteRule substitution then Apache will implicitly trigger an external redirect.


To make the above target a specific domain (eg. local.example.com), you just need to add an additional condition that checks the Host header. For example:

RewriteCond %{HTTP_HOST} ^local\.example\.com
RewriteCond %{REQUEST_URI} ^/wp-content/uploads/[^/]+/
RewriteRule (.*) http://example.com/$1 [R,QSA,L]

Some notes about the above:

  • (.*) is the same as ^(.*)$ – the anchors are unnecessary.
  • Likewise, .*$ (which simply matches anything at the end of the URL) is superfluous.
  • No need to backslash escape slashes in the character class: [^/] is the same as [^\/].
  • Match 1 or more characters in the path segment, not 0 or more. ie. [^/]+ instead of [^/]*.
  • I’ve added the R, since you might as well make it clear what is really happening.

But, you don’t actually need the second RewriteCond that matches against the REQUEST_URI. It is more efficient to move this to the RewriteRule pattern, since the RewriteRule pattern is processed first. For example, the above could be rewritten as:

RewriteCond %{HTTP_HOST} ^local\.example\.com
RewriteRule ^wp-content/uploads/[^/]+/ http://example.com%{REQUEST_URI} [R,QSA,L]

Leave a Comment