Why is my `favicon.ico` request not working?

房东的猫 提交于 2019-12-22 01:28:05

问题


I have a favicon.ico on my site.

In the HTML, I link to its location...

<link rel="icon" href="/assets/images/layout/favicon.ico" type="image/x-icon" />

I also have this in my .htaccess.

# Redirect /favicon.ico requests
RewriteCond %{REQUEST_URI} !^assets/images/layout/favicon\.ico [NC]
RewriteCond %{REQUEST_URI} ^favicon\.(gif|ico|png|jpe?g)$ [NC]
RewriteRule ^(.*)$ assets/images/layout/favicon.ico [R=301,L]

...to redirect the /favicon.ico requests to a different location.

For some reason, every time I request favicon.ico in my browser, I get 304 Not Modified response with matching Etags and apparently a blank image, even though /assets/images/layout/favicon.ico does exist.

I get the same issue when trying to access it wil the full path.

What is going on here? What is causing this 304?


回答1:


First of all I would rather put this rule in .htaccess like this:

RewriteRule ^favicon\.(gif|ico|png|jpe?g)$ /assets/images/layout/favicon.ico [L,NC]

Then if you have this in your web page:

<link rel="icon" href="/favicon.ico" type="image/x-icon" />

/favicon.ico will be internally redirected to /assets/images/layout/favicon.ico

I have tested this in IE, Firefox and Chrome and all 3 show this behavior that for the first time (or after clearing cache) I get 200 for favicon.ico but afterwards all browsers cache this icon file and don't bother to send another request to the server. With this setup I didn't find any 304 happening in my testing.

My access log:

Chrome

127.0.0.1 - - [05/May/2011:23:58:15 -0400] "GET /favicon.ico HTTP/1.1" 200 1150 "-" "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/534.24 (KHTML, like Geck
o) Chrome/11.0.696.60 Safari/534.24"

IE

127.0.0.1 - - [06/May/2011:00:05:18 -0400] "GET /favicon.ico HTTP/1.1" 200 1150 "-" "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .
NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; .NET CLR 3.0.04506.648; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)"

Firefox

127.0.0.1 - - [06/May/2011:00:07:33 -0400] "GET /favicon.ico HTTP/1.1" 200 1150 "-" "Mozilla/5.0 (Windows NT 5.1; rv:2.0.1) Gecko/20100101 Firefox/4.0.1"



回答2:


<IfModule mod_rewrite.c>
RewriteRule ^favicon\.ico$ _/img/ico/favicon.ico [L]
RewriteRule ^apple-touch-icon\.png$ _/img/ico/apple-touch-icon.png [L]
RewriteRule ^apple-touch-icon-precomposed\.png$ _/img/ico/apple-touch-icon-precomposed.png [L]
RewriteRule ^apple-touch-icon-57x57-precomposed\.png$ _/img/ico/apple-touch-icon-57x57-precomposed.png [L]
RewriteRule ^apple-touch-icon-72x72-precomposed\.png$ _/img/ico/apple-touch-icon-72x72-precomposed.png [L]
RewriteRule ^apple-touch-icon-144x144-precomposed\.png$ _/img/ico/apple-touch-icon-144x144-precomposed.png [L]
RewriteRule ^apple-touch-icon-114x114-precomposed\.png$ _/img/ico/apple-touch-icon-114x114-precomposed.png [L]



来源:https://stackoverflow.com/questions/5906292/why-is-my-favicon-ico-request-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!