htaccess-将图像请求重定向到单个文件

时间:2018-11-24 20:17:13

标签: apache .htaccess mod-rewrite url-rewriting

我正在尝试配置 .htaccess 以将404图片请求重定向到单个文件: 404.jpg 这是我的代码:

# If there's no file at the requested path..
RewriteCond %{REQUEST_FILENAME} !-f
# Then, if it's an image file, try the other image:
RewriteRule /([^/]*\.(gif|png|jpg|svg|JPG|PNG|JPEG|jpeg))$ https://example.com/images/404.jpg

这很好用:

http://example.com/upload/images/a/ac/UserAvatar.png

但是它不适用于

http://example.com/upload/images/thumb/a/ac/UserAvatar.png/100px-UserAvatar.png

有解决方案吗?干杯

1 个答案:

答案 0 :(得分:0)

这应该可以完成工作:

RewriteEngine On
RewriteCond %{REQUEST_URI} \.(gif|png|jpg|svg|jpeg)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ https://example.com/images/404.jpg [L]

检查是否启用了mod_rewrite

apachectl -M | grep rewrite

和AllowOverride已激活

<Directory /var/www/html/>
    AllowOverride All
</Directory>

祝你好运!