1

I work on a multisite WordPress installation (see - https://codex.wordpress.org/Create_A_Network), I don't want to have to download all the remote production images to test the sites on my local environment.

The following .htaccess to rewrite example allows images to be pulled direct from a live WordPress site:

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

See - https://css-tricks.com/develop-locally-use-images-production/, this describe the normal standalone WordPress installation.

Can anyone adapt this to work for a Multisite install i.e. one that encompasses multiple WordPress blogs on different domains?

I've tried the following, to map requests for specific local sub-domains, but no success:

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

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]
  • 1
    Thanks for a brilliant answer and thorough explanation! (Sorry for my delay in marking correct - I've been on other projects and not had a chance to check until today.) – benedict_w Apr 10 '18 at 9:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.