.htaccess - 重定向网站图标
如何将根目录或任何子目录中对 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
试试这个规则:
编辑 对于具有任意路径段深度的favicon.ico:
Try this rule:
Edit And for favicon.ico with arbitrary path segment depth:
要获取网站图标,请访问
www.mysite.com/images/favicon.ico
最可靠的方法是:
解释:
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/
文件夹中的 xxxFor a favicon at
www.mysite.com/images/favicon.ico
the most robust method would be:
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 wordfavicon
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 previousRewriteCond
. Note that the%1
is a called a RewriteCond backreference this means it is the lastRewriteCond
that has been met. (eg%2
would be the 2nd-lastRewriteCond
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 orL
the RewriteRule will result in a permanent loop (since the redirect URL will keep satisfying theRewriteRule
conditions). However, it's a good idea to add theL
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, aRewriteRule (.*) xxx
type rule in yourwww.mysite.com/images/
folder can mess with the results.我知道问题被标记为
.htaccess
但是,为什么不使用符号链接呢?I know the question is tagged
.htaccess
but, why not use a symlink?这个快速重写应该可以解决问题:
This quick rewrite should do the trick: