为什么我的“favicon.ico”请求不起作用?
我的网站上有一个 favicon.ico
。
在 HTML 中,我链接到它的位置...
<link rel="icon" href="/assets/images/layout/favicon.ico" type="image/x-icon" />
我的 .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]
...将 /favicon.ico
请求重定向到不同的位置。
由于某种原因,每次我在浏览器中请求 favicon.ico
时,我收到 304 Not Modified
响应,其中包含匹配的 Etag,并且显然是一张空白图像,尽管 /assets/images/layout/favicon.ico
确实存在。
我在尝试使用完整路径访问它时遇到了同样的问题。
这是怎么回事?是什么导致了这个304
?
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
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,我宁愿将此规则放入 .htaccess 中,如下所示:
然后,如果您的网页中有此内容:
/favicon.ico
将内部重定向到/assets/images/layout/favicon.ico
我已经在 IE、Firefox 和 Chrome 中对此进行了测试,所有 3 个浏览器都显示了此行为,第一次(或清除缓存后)我得到
200 for favicon.ico 但之后所有浏览器都会缓存此图标文件,并且不会向服务器发送另一个请求。通过此设置,我在测试中没有发现任何
304
发生。我的访问日志:
Chrome
IE
Firefox
First of all I would rather put this rule in .htaccess like this:
Then if you have this in your web page:
/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 any304
happening in my testing.My access log:
Chrome
IE
Firefox