设置字体过期头无效

26
我在我的.htaccess文件中使用以下代码来尝试设置某些字体的过期标头,但在检查火狐浏览器缓存和到期标头时,该字体设置为从现在起约12小时后到期,而不是我尝试设置的1年。

这是我的代码:

# Add correct content-type for fonts
AddType application/vnd.ms-fontobject .eot
AddType application/x-font-ttf .ttf
AddType application/x-font-opentype .otf
AddType application/x-font-woff .woff
AddType image/svg+xml .svg

# Compress compressible fonts
AddOutputFilterByType DEFLATE application/x-font-ttf application/x-font-opentype image/svg+xml

# Add a far future Expires header for fonts
ExpiresByType application/vnd.ms-fontobject "access plus 1 year"
ExpiresByType application/x-font-ttf "access plus 1 year"
ExpiresByType application/x-font-opentype "access plus 1 year"
ExpiresByType application/x-font-woff "access plus 1 year"
ExpiresByType image/svg+xml "access plus 1 year"

我做错了什么?


1
@jww 这个评论真的有必要在5年后发出吗?如果你如此关心“主题相关”的问题,也许你应该去挑拨一些新的问题! :) - Brett
问题是,人们看到这个问题然后问类似的问题。我知道的唯一标志不适合主题的方法是自定义关闭消息。 - jww
3
@jww ,.htaccess是相关主题。请参阅此FAQ文章 - Papershine
1
@paper1111 - 在这里,一个问题必须同时满足“程序员常用的软件工具”和“实际可回答的与软件开发相关的独特问题”的要求才能被视为符合主题。Apache配置文件显然不满足后者。其他任何想法都只是一厢情愿的幻想。 - jww
3
我会尽力做到最好。以下是翻译的结果:@jww,我支持 paper1111 的观点。根据那个元问题,这是可以接受的。此外,这也是为什么很多人对 @ SO 的某些人感到恼火的原因 - 因为有太多过于挑剔的人在高喊某些问题不合适或重复。 - Brett
2个回答

78

看起来我还得包括这一部分:

ExpiresActive on

完整代码如下:

# Add correct content-type for fonts
AddType application/vnd.ms-fontobject .eot
AddType font/ttf .ttf
AddType font/otf .otf
AddType font/woff .woff
AddType font/woff2 .woff2
AddType image/svg+xml .svg

# Compress compressible fonts
AddOutputFilterByType DEFLATE font/ttf font/otf image/svg+xml

ExpiresActive on

# Add a far future Expires header for fonts
ExpiresByType application/vnd.ms-fontobject "access plus 1 year"
ExpiresByType font/ttf "access plus 1 year"
ExpiresByType font/otf "access plus 1 year"
ExpiresByType font/woff "access plus 1 year"
ExpiresByType font/woff2 "access plus 1 year"
ExpiresByType image/svg+xml "access plus 1 year"

那对我也起作用了。 不过奇怪的是,我只是简单地添加了一个ExpiresDefault“access plus 1 month”,它可以正确处理所有文件类型(html、jpg、css、js等),但svg除外。一旦我添加了ExpiresActive,ExpiresDefault也适用于svg。 有什么想法吗? - Ariel Allon
很难说为什么你没有使用ExpiresActive也能正常工作,但是以后一定要确保始终使用它来确保。 :) - Brett
1
对于建议使用的woff2,请添加以下代码:AddType application/font-woff2 .woff2ExpiresByType application/font-woff2 "access plus 6 month" - user9112752
@BO41 谢谢你的回复,但是你有没有想过为什么这个是 font-woff2 而不是像其他的一样是 x-font-woff2 - Brett
@Brett 不好意思,我不知道。也许其他人也可以不用 "x-",或者这是浏览器特定的问题。在网上没有找到太多信息。我只知道在最新的 Chromium 和 Firefox 中可以这样工作。 也许你可以在网上找到一些信息(https://dev59.com/fXA65IYBdhLWcg3w2yk2)。 - user9112752
@BO41 谢谢。在我的研究中,我发现.ttf.otf的MIME类型似乎已经改变,但是我还没有测试它们,所以在测试之前我对更新解决方案持谨慎态度。 - Brett

2

我相信你的问题在于如何定义字体,而不需要添加它们的内容类型。

 ExpiresByType font/truetype "access plus 1 year"
 ExpiresByType font/opentype "access plus 1 year"
 ExpiresByType application/x-font-woff   "access plus 1 year"
 ExpiresByType image/svg+xml "access plus 1 year"
 ExpiresByType application/vnd.ms-fontobject "access plus 1 year"

我会尝试,但是我从这里得到了那个定义:https://dev59.com/6XE85IYBdhLWcg3wPBB8 - Brett
好的,我刚刚测试了一下,但是没有任何变化。 :( - Brett

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接