![仅当图像不存在时才使用 .htaccess 重写](https://linux22.com/image/632121/%E4%BB%85%E5%BD%93%E5%9B%BE%E5%83%8F%E4%B8%8D%E5%AD%98%E5%9C%A8%E6%97%B6%E6%89%8D%E4%BD%BF%E7%94%A8%20.htaccess%20%E9%87%8D%E5%86%99.png)
如何让 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