仅当图像不存在时才使用 .htaccess 重写

仅当图像不存在时才使用 .htaccess 重写

如何让 RewriteCond+RewriteRule 将 site1.com/folder1 更改为 site2.com/folder1,仅当此目录中的文件不存在时。例如:site1.com/wp-content/uploads/2014/03/image.jpg

如果图像 site1.com/wp-content/uploads/2014/03/image.jpg 不存在,则获取 site2.com/wp-content/uploads/2014/03/image.jpg 但如果图像存在,则不执行任何操作。

更新,这是我需要的代码,谢谢大家的帮助)

Options +FollowSymLinks
RewriteEngine ON
RewriteCond %{REQUEST_URI} "/uploads/"
RewriteCond %{REQUEST_FILENAME} .*\.(jpeg|jpg|gif|png)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.+) http://site2.com/$1 [L,R=301]

答案1

尝试这样的事情。

RewriteCond %{REQUEST_FILE} !-f RewriteRule ^(.*)$ http://site2.com/$1 [QSA,R,L]

答案2

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
# the above ignores any url for a file that exists on the site
RewriteCond %{REQUEST_FILENAME} !-d
# the above ignores any url for a directory that exists on the site
RewriteRule ^(.*)$ http://[NEW_DOMAIN]/$1 [QSA,R,L]
# Redirect to another site and remain the same URL

相关内容