Apache 在特定 URL 模式下出现 404 错误时显示默认图像

Apache show a default image when there is a 404 error under a specific URL pattern

在纯基于客户端的 Web 应用程序中,我需要显示有时不存在的背景图像。当特定 URL 模式 return 出现 404 错误时,'forcing' Apache 是否有办法提供图像?

可能 return 404 的 URL 模式是:

http://host/assets/user-images/xxxxx.jpg

我想在哪里提供图像:

http://host/assets/user-images/default.jpg

注意:

我已经在使用 .htaccess

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [QSA,L]

在文件“/assets/user-images/.htaccess”中添加以下内容

RewriteCond %{REQUEST_URI} ^/assets/user-images/(.*)\.jpg$ 
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ default.jpg

第一个 RewriteCond 将检查传入请求是否针对 /assets/user-images/ 下的 jpg 文件,第二个带有 -f 选项的 RewriteCond 将检查请求的文件是否存在。如果不存在,RewriteRule 将提供默认图像。 最终用户仍会看到原始图像的 URL,但会提供默认图像。 如果原始图像文件存在,则不会执行此规则。