.htaccess规则堆叠和404重定向

时间:2012-02-17 15:37:52

标签: .htaccess

用户请求PNG图片,例如:http://server/myfolder/subfolder/1234.png。如果.png不存在,那么我想在相同名称的同一文件夹中显示.gif,但.gif扩展名应该存在。

另一个补充是我们最近更改了目录结构,因此我需要检查的URL可能已更改。例如,应该可以通过http:// server/myfolder/subfolder/1234.pnghttp://server/oldfolder/1234.png直接请求上述网址。目录结构的变化可表示为:

RewriteRule oldfolder/(.*) myfolder/subfolder/$1 [L,QSA]

在这两个目录中,我需要检查文件是否存在,并在必要时使用其他图像。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:2)

尝试将以下内容添加到您网站根目录中的.htaccess文件中。

它也应该适用于您更改的文件结构。

RewriteEngine on
RewriteBase / 

#if the file does not exist
RewriteCond %{REQUEST_FILENAME} !-f
#and is in any of these folders: /myfolder/subfolder or /folder1 or /folder2 
RewriteCond %{REQUEST_URI} ^/(myfolder/subfolder|folder1|folder2)/ [NC] 
#and its a png, then try to serve the gif instead
RewriteRule ^(.+)\.png$ $1.gif [L,NC]