在 htaccess 中使用过期标头配置图标

12 .htaccess favicon

我已经通过我的 Apache 服务器上的 mod_expires 实现了 Expires 标头,并且已经成功地为大多数文件类型创建了 expires 标头,但是我在使用 favicon 时遇到了问题。

我最初尝试为 .ico 文件添加一组指令,但是当通过 YSlow 检查时,它没有设置过期时间。

当这不起作用时,我将其修改为 GIF,但这似乎有同样的问题。

网站上的其他 gif 已正确设置过期标题(至少根据 YSlow)。

任何人都可以对这种情况有所了解吗?

小智 20

如果您使用的是 Apache,只需在您网站的 .htaccess 文件或 Apache 的 mime.types 文件中为图标添加 MIME-Type:

# Add Proper MIME-Type for Favicon
AddType image/x-icon .ico
Run Code Online (Sandbox Code Playgroud)

然后,在未来几个月设置 Expires 标头:

# Enable Apache's mod_expires Module
ExpiresActive On
ExpiresByType image/x-icon "access plus 1 month"
Run Code Online (Sandbox Code Playgroud)

清除浏览器缓存并重新加载页面后,您应该会看到 favicon.ico 文件的缓存控制标头设置为未来的日期(以秒为单位):

Content-Type image/x-icon
Cache-Control: max-age=2592000
Run Code Online (Sandbox Code Playgroud)

有关相关信息,请参阅 YSlow 文档关于使您的 favicon.ico 小且可缓存http : //developer.yahoo.com/performance/rules.html#favicon