将以字母 a 开头的不存在的图像重定向到 no-photo-A.jpg

Redirect non existing images that begins with letter a to no-photo-A.jpg

我正在尝试将所有不存在的图像重定向到 np-photo.jpg,但我需要一些多样化:

我有代码:

Options +FollowSymLinks
RewriteEngine ON
RewriteCond %{REQUEST_URI} "/images/"
RewriteCond %{REQUEST_FILENAME} ^a.*\.jpg(jpeg|jpg|gif|png)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.+) /images/no-photo-A.jpg [L,R=301] 

问题在于:

RewriteCond %{REQUEST_FILENAME} ^a.*\.(jpeg|jpg|gif|png)$

如果我删除字母 a 那么它会匹配所有请求,但我不能强制它只匹配以特定字母开头的文件名。

https://domainname.pl/images/a11.jpg -should be redirected
https://domainname.pl/images/b11.jpg -should not be redirected

您可以像这样使用一个规则:

Options +FollowSymLinks
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^images/([A-Z0-9])[\w-]*\.(?:jpe?g|gif|png)$ /images/no-photo-.jpg [L,R=301,NC,NE]

这会将不存在的 /images/A-foo-bar.jpg 重定向到 /images/no-photo-A.jpg

使用您显示的示例,请尝试遵循 htaccess 规则。确保您的 htaccess 文件与图像文件夹一起存在(不在其中)。

确保在测试您的 URL 之前清除您的浏览器缓存。

Options +FollowSymLinks -MultiViews
RewriteEngine ON
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}//no-photo-.jpg [NC]
RewriteRule ^(images)/(?:A|B)(.*)\.(?:jpeg|jpg|gif|png)$/?$ /no-photo-.jpg [R=301,NC,L]