开发者

.htaccess forward local to remote on 404

开发者 https://www.devze.com 2023-03-04 01:39 出处:网络
I do all of my development locally on sites that use a CMS. We have a directory that is used to store user-uploaded content, such as images (/assets/). The problem is, when developing locally, I don\'

I do all of my development locally on sites that use a CMS. We have a directory that is used to store user-uploaded content, such as images (/assets/). The problem is, when developing locally, I don't want all of the uploaded files from the production site on my machine, so I leave this directory empty, and all of the HTTP requests for files in the /assets/ directory get 404'ed.

What would be great is if I could have a rewrite rule in my .htaccess that detects the 404, and forwards to the external URL of the production site to load the asset from there. The logic would be:

Request localhost/somesite/assets/foo.jpg
200 response ? send the local file /somesite/assets/foo.jpg
404 ? forward to http://www.开发者_运维百科productionsite.com/assets/foo.jpg

Is this possible?


Have your .htaccess rules like this:

Options +FollowSymlinks -MultiViews
RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(assets/.*)$ http://www.productionsite.com/$1 [R,L,NC]

This will make sure that any request matching /assets/ and doesn't already exist on your local webserver will be externally redirected to http://www.productionsite.com/assets/...

btw you should really improve your acceptance rate otherwise you may not many answers here.


Is /assets always on another site, or only if the files are not available locally?

The reason I ask is that it may be simpler to always redirect just /assets to the live with the following (untested) rules in httpd.conf/.htaccess on your development server:

Options +FollowSymLinks
RewriteEngine on
RewriteCond $1 ^(assets) [NC]
RewriteRule ^(.*)$ http://www.livesite.com/$1 [L]
0

精彩评论

暂无评论...
验证码 换一张
取 消