【问题标题】:Using mod_rewrite to call in a default image URL if requested image URL is not found如果未找到请求的图像 URL,则使用 mod_rewrite 调用默认图像 URL
【发布时间】:2017-08-14 14:54:19
【问题描述】:

我对 htaccess 重写相当陌生,想知道是否有人可以为我提供解决方案。因此,在我的网站上,我的用户具有以下格式的自定义头像图像。

http://siteurl.tld/customavatars/avatar78364_1.gif (其中数字 (78364_1) 是用户 ID)

每个用户都会生成自己的自定义头像图片链接,即使他们没有上传图片。这是问题所在,目前我正在使用 ErrorDocument 404 /images/icons/no-avatar.png 作为没有上传自己图像的任何用户的默认图像。这个 404 对 SEO 不利,所以我想尝试使用重写来保留原始 URL,但提供无头像图像文件。

实际上,任何访问http://siteurl.tld/customavatars/avatarUSERID_HASNOIMAGE.gif 的人都只会显示默认图像,而不会被重定向到浏览器 URL 中的 no-avatar.png。

【问题讨论】:

  • 我的回答对你有用吗?希望得到简短的反馈:)

标签: apache .htaccess mod-rewrite centos http-status-code-404


【解决方案1】:

这应该可以解决问题:

RewriteCond %{REQUEST_FILENAME} avatar.*\.gif$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule /.* http://siteurl.tld/customavatars/no-avatar.png [R=301,L]

实际上,任何以头像开头并以 .gif 结尾但不存在的请求文件都会以http://siteurl.tld/customavatars/no-avatar.png 回答

希望对你有帮助:)

【讨论】:

  • 刚刚试了一下,效果不错。谢谢你的帮助!!
猜你喜欢
  • 2016-05-20
  • 2020-08-16
  • 2023-02-05
  • 2015-06-03
  • 1970-01-01
  • 2020-11-25
  • 2016-10-20
  • 2014-01-30
  • 2019-10-23
相关资源
最近更新 更多