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:
- Checks that you are on the dev server first (
HTTP_HOST
) - Checks that the requested URL is in wp-content/uploads (
REQUEST_URI
) - Checks that the requested URL does not exist locally (file or directory via
REQUEST_FILENAME
) - 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:
- 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
- Image A shows up, Image B gets a 404 error (as expected)
- Add the
.htaccess
snippet after WordPress stuff - Nothing changes
- 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.