htaccess to load assets from staging URL when file

2019-06-08 13:05发布

I'm trying to load images from a staging site if they don't exist locally. This is basically useful so I don't have to keep downloading all the images that the client uploads on the staging site. At the moment, what I have is working, but it's an all-or-nothing solution. So, all the images are loading remotely but I'd like htaccess to only load remote images if a local one doesn't exist.

Here's what I have:

RewriteCond %{HTTP_HOST} ^local-site\.dev$
RewriteRule ^.*/uploads/(.*)$ http://remote-site.com/wp-content/uploads/$1 [L,R=301]

Thanks in advance.

1条回答
SAY GOODBYE
2楼-- · 2019-06-08 13:36

Change your rule to this:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{HTTP_HOST} ^local-site\.dev$
RewriteRule ^.*/(uploads/.*)$ http://remote-site.com/wp-content/$1 [L,R=301,NC]

Here RewriteCond %{REQUEST_FILENAME} !-f will make sure that this redirection happens only when image file doesn't exist locally.

查看更多
登录 后发表回答