如果以前的文件不存在,Htaccess重定向到另一个文件


Htaccess redirect to another file if previous not exists

我正在将一个站点从codeigniter转换为laravel我想将我以前的图像URL重定向到新的,每个资源的图像都是单个的,所以我使用了资源ID

上一个URL示例http://www.example.org/application/images/280x280/1.png

新URL示例http://www.example.org/resources/1/cover1.png

其中1是资源ID

我的重定向Htaccess

# Redirect Cover Images
RewriteCond %{HTTP_HOST} ^(.*)'.example(.*).org [NC]
RewriteRule ^application/images/(.*)/(.*).(jpg|png) http://example%2.org/resources/$2/cover1.png [R=301,L]

在新的网站上,我有三种类型的图像

cover1.png,cover2.png,default_cover.png

路径保持相同的

上面的.htaccess代码适用于cover1.png大小写,

我需要编写重定向,如果cover1.png不存在,它将重定向到cover2.png。如果不存在,则重定向到default_cover.png,默认cover的路径是不同的

http://www.example.org/resources/default/default_cover.png

我如何使用.htaccess 来实现这一点

您可以使用以下条件规则:

#1)if /cover1.png !=existent file
RewriteCond %{DOCUMENT_ROOT}/cover1'.png !-f
#redirect "/cover1.png" to "/cover2.png"
RewriteRule ^ /cover2'.png [NC,L,R]
#2)if /cover2.png !=existent file
RewriteCond %{DOCUMENT_ROOT}/cover2'.png !-f
#redirect "/cover2.png" to "/default.png"
RewriteRule ^ /default.png [NC,L,R]