How to proxy local WP uploads folder to live site

Stack:

MAMP PRO on OSX 10.11.5 (El Cap)

Scenario:

I’m developing a site locally, and there is already a live version of the site online. Rather than download all 4gb of the uploads folder, I’d like to have my local site just load the images from the live URL instead.

Concept:

Through lots of searching, I’ve found many variations of .haccess files you can use to accomplish this. My favorite ones use the following conditions:

  1. Checks that you are on the dev server first (HTTP_HOST)
  2. Checks that the requested URL is in wp-content/uploads (REQUEST_URI)
  3. Checks that the requested URL does not exist locally (file or directory via REQUEST_FILENAME)
  4. If these pass, then execute the rewrite rule.

Code:

Here’s my .htaccess snippet:

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteBase /wp-content/uploads/
  RewriteCond %{HTTP_HOST} ^LOCALSITE\.dev$ [NC]
  RewriteCond %{REQUEST_URI} ^/wp-content/uploads/
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteRule ^(.*)$ http://LIVESITE.com/wp-content/uploads/$1 [NC,P]
</IfModule>

FOR THE LIFE OF ME I CANNOT GET THIS TO WORK RIGHT

I’ve run as many tests as I can think of, and have verified that mod_rewrite is ON in MAMP. I’ve read more htaccess documentation that I care for, and I’m going in circles now.

Test:

One of my tests leaves me confused. Here’s what I did:

  1. Create a page in WordPress with 2 images in the content.
    • “Image A” exists on my local machine only
    • “Image B” exists on the LIVE site only, but is using my local hostname in the URL
  2. Image A shows up, Image B gets a 404 error (as expected)
  3. Add the .htaccess snippet after WordPress stuff
  4. Nothing changes
  5. But if I comment out all the RewriteCond statements, Image A 404’s because it is the only one which actually gets rewritten with the LIVE URL. It doesn’t exist there so yeah it 404’s. But Image B’s URL has not been rewritten and it 404’s also.

Disclaimer: It’s 4am

Any help is appreciated, thanks.

1 Answer
1

So I discovered the answer after reading through my question after I posted it….

The htaccess rules need to go BEFORE the WordPress rules

There you have it. That was the problem this entire time.

I’m going to bed.

Leave a Comment