.htaccess - 重定向网站图标

发布于 2024-08-24 00:15:45 字数 63 浏览 5 评论 0原文

如何将根目录或任何子目录中对 favicon.ico 的所有请求重定向到 /images/favicon.ico

How do I redirect all requests for favicon.ico in root directory or any subdirectory to /images/favicon.ico

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

壹場煙雨 2024-08-31 00:15:45

试试这个规则:

RewriteEngine on
RewriteRule ^favicon\.ico$ /images/favicon.ico [L]

编辑    对于具有任意路径段深度的favicon.ico

RewriteCond $0 !=images/favicon.ico
RewriteRule ^([^/]+/)*favicon\.ico$ /images/favicon.ico [L]

Try this rule:

RewriteEngine on
RewriteRule ^favicon\.ico$ /images/favicon.ico [L]

Edit    And for favicon.ico with arbitrary path segment depth:

RewriteCond $0 !=images/favicon.ico
RewriteRule ^([^/]+/)*favicon\.ico$ /images/favicon.ico [L]
笑红尘 2024-08-31 00:15:45

要获取网站图标,请访问 www.mysite.com/images/favicon.ico
最可靠的方法是:

RewriteCond %{REQUEST_URI} !^/images/favicon\.ico$ [NC]
RewriteCond %{HTTP_HOST} (.+)
RewriteRule ^(.*)favicon\.(ico|gif|png|jpe?g)$ http://%1/images/favicon.ico [R=301,L,NC]

解释:

RewriteCond %{REQUEST_URI} !^/images/favicon\.ico [NC] :
- 确保如果请求正确的 URI,则重定向规则不适用(例如,301 重定向会将正确的网站图标 URI 写入浏览器缓存 - 如果浏览器请求正确的 URI,此行将避免处理该规则)
- [NC] 表示不区分大小写

RewriteCond %{HTTP_HOST} (.+) :
- 检索 http 主机名 - 避免将主机名硬编码到 RewriteRule
- 这意味着您可以毫无问题地在本地/测试服务器和生产服务器之间复制 .htaccess 文件(或者需要将新站点基本 url 重新硬编码到 RewriteRule 中)

RewriteRule ^(.*)favicon\.( ico|gif|png|jpe?g)$ http://%1/images/favicon.ico [R=301, L] :
- ^ 是正则表达式的开头
- (.*) 是通配符组 - 这意味着 URI 中的单词 favicon 之前可以有零个或任意数量的字符(即,这是允许根目录或任何子目录包含在 URI 匹配中)
- \.(ico|gif|png|jpe?g) 检查 URI 扩展名是否与 .ico.gif、<代码>.png、<代码>.jpg、<代码>.jpeg
- $ 是正则表达式的结尾
- http://%1/images/favicon.ico 是重定向网址 - 它注入我们在前面的 RewriteCond 中检索到的主机名。请注意,%1 被称为 RewriteCond反向引用 这意味着它是最后一个已满足的RewriteCond。 (例如 %2 将是已满足的倒数第二个 RewriteCond
- R=301 表示它是永久重定向 - 将重定向存储在浏览器缓存中。测试时要小心 - 您需要在代码更改之间删除浏览器缓存,否则重定向将不会更新。在您知道该规则有效之前,可能会忽略此内容。
- L 表示它是此 .htaccess 文件中要遵循的最后一个重定向 - 您不需要它来使规则正常工作,因为第 1 行不会满足一次浏览器将定向到正确的 url。如果没有第 1 行或 L ,RewriteRule 将导致永久循环(因为重定向 URL 将继续满足 RewriteRule 条件)。但是,如果您有其他遵循 favicon 规则的规则,那么添加 L 是个好主意 - 因为在 favicon.ico 请求中,您(可能)可以忽略任何以下规则。

您可以在 http://htaccess.mwl.be/ 测试 .htaccess 规则

最后说明:
- 请注意,您的任何子目录中的 .htaccess 文件中都没有任何其他 RewriteRule。
- 例如,如果您将这个答案放入您的www.mysite.com/根文件夹.htaccess文件中,则RewriteRule (.* ) www.mysite.com/images/ 文件夹中的 xxx 类型规则可能会扰乱结果。

For a favicon at www.mysite.com/images/favicon.ico
the most robust method would be:

RewriteCond %{REQUEST_URI} !^/images/favicon\.ico$ [NC]
RewriteCond %{HTTP_HOST} (.+)
RewriteRule ^(.*)favicon\.(ico|gif|png|jpe?g)$ http://%1/images/favicon.ico [R=301,L,NC]

Explanation:

RewriteCond %{REQUEST_URI} !^/images/favicon\.ico [NC] :
- ensures that the redirect rule does NOT apply if the correct URI is requested (eg a 301 redirect will write the correct favicon URI to browser cache - and this line avoids processing the rule if the browser requests the correct URI)
- [NC] means it's not case sensitive

RewriteCond %{HTTP_HOST} (.+) :
- retrieves the http host name - to avoid hard coding the hostname into the RewriteRule
- this means you can copy your .htaccess file between local/test server and production server without problems (or the need to re-hardcode your new site base url into your RewriteRule)

RewriteRule ^(.*)favicon\.(ico|gif|png|jpe?g)$ http://%1/images/favicon.ico [R=301, L] :
- ^ is the start of the regex
- (.*) is a wildcard group - which means that there can be zero or any number of characters before the word favicon in the URI (ie this is the part that allows root directory or any subdirectories to be included in the URI match)
- \.(ico|gif|png|jpe?g) checks that the URI extension matches any of .ico, .gif, .png, .jpg, .jpeg
- $ is the end of the regex
- http://%1/images/favicon.ico is the redirect url - and it injects the hostname we retrieved in the previous RewriteCond. Note that the %1 is a called a RewriteCond backreference this means it is the last RewriteCond that has been met. (eg %2 would be the 2nd-last RewriteCond that to have been met)
- R=301 means it's a permanent redirect - which stores the redirect in the browser cache. Be careful when testing - you'll need to delete browser cache between code changes or the redirect won't update. Probably leave this out until you know the rule works.
- L means its the last redirect to be followed in this .htaccess file - you won't need this to get the rule working since line 1 won't be met once the browser is directed to the correct url. Without the either line 1 or L the RewriteRule will result in a permanent loop (since the redirect URL will keep satisfying the RewriteRule conditions). However, it's a good idea to add the L anyway if you have other rules following the favicon rules - since on a favicon.ico request, you can (probably) ignore any following rules.

You can test .htaccess rules at http://htaccess.mwl.be/

Final Note:
- be careful that you don't have any other RewriteRule in an .htaccess file located in any of your sub-directories.
- eg if you put this answer in your www.mysite.com/ root folder .htaccess file, a RewriteRule (.*) xxx type rule in your www.mysite.com/images/ folder can mess with the results.

忆梦 2024-08-31 00:15:45
RewriteEngine on

RewriteRule ^(.*)favicon\.ico /images/favicon.ico [L]
RewriteEngine on

RewriteRule ^(.*)favicon\.ico /images/favicon.ico [L]
ぃ双果 2024-08-31 00:15:45

我知道问题被标记为 .htaccess 但是,为什么不使用符号链接呢?

ln -s images/favicon.ico favicon.ico

I know the question is tagged .htaccessbut, why not use a symlink?

ln -s images/favicon.ico favicon.ico
-小熊_ 2024-08-31 00:15:45

这个快速重写应该可以解决问题:

RewriteRule ^(.*)favicon.ico /images/favicon.ico

This quick rewrite should do the trick:

RewriteRule ^(.*)favicon.ico /images/favicon.ico
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文