apache - 通过 .htaccess 设置过期 header 和 gzipping 数据

标签 apache .htaccess httpd.conf webfonts expires-header

我正在尝试对其进行设置,以便浏览器可以长时间缓存网络字体,并尝试对它们进行 gzip 以加快下载速度。

据我所知,您可以通过您的 httpd.conf 来做到这一点。文件在 Apache 或通过 .htaccess .

不过,我不确定如何判断 gzip 是否已启用,我阅读了有关搜索 httpd.conf 的内容。文件为 DEFLATE ;我这样做了,但什么也没找到 - 所以不确定它是否启用!?

无论如何,我已将此代码放在下面的 .htaccess 中文件,部分是因为我不知道将它放在 httpd.conf 文件的什么位置,部分是因为在 .htaccess 文件中进行更改更容易,而不会持续打扰我的主机。

这是代码..

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

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

# 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/x-woff "access plus 1 year"
ExpiresByType image/svg+xml "access plus 1 year"

现在,我想知道这是否正确,因为我也看到了类似但不同的到期代码。
ExpiresByType application/x-font-ttf "access plus 1 month"
ExpiresByType font/opentype "access plus 1 month"
ExpiresByType application/x-font-woff "access plus 1 month"
ExpiresByType image/svg+xml "access plus 1 month"
ExpiresByType application/vnd.ms-fontobject "access plus 1 month"

将它放在 .htaccess 中的哪个位置是否重要?文件还是可以去任何地方?

最佳答案

ExpiresByType 可以进入您的 httpd.conf、虚拟主机配置或 htaccess,如每个指令条目顶部的 apache 文档概述框中所示:

Apache Documentation screenshot

http://httpd.apache.org/docs/2.2/mod/mod_expires.html#expiresbytype

上下文列表是您如何判断可以将各种 Apache 指令类型放入哪些配置文件的方法。

假设您使用的是 PHP,您的 phpinfo 详细信息应该会告诉您是否启用了 gzip。

PHPInfo Screenshot

编辑

为了回答您关于字体的正确 MIME 类型的问题,就它们与 Apache 服务器的文件关联而言,我的 mime.types 文件(在您的 httpd.conf 所在的 apache conf 文件夹中找到)告诉我

application/x-font-(extension)

将是与 ExpiresByType 一起使用的关联。我建议先让字体工作,然后检查字体 url 的标题,看看它是否给出了正确的到期日期。

enter image description here

对于接收网页字体的浏览器,this article似乎表明未考虑 MIME 类型,因此无关紧要。但是this one似乎与此相矛盾。据我了解,没有官方字体/MIME 类型,因此任何浏览器实现都在 MIME 规范之外。

相关问题:

Proper MIME type for fonts

Correct Apache AddType directives for font MIME types

我在 .htaccess 中没有看到任何指定放置要求的内容,因此您应该安全地将这些规则放在您认为合适的任何地方,尽管如果您执行 AddTypes,您需要在引用声明的 Type 的任何进一步指令之上执行此操作。最好将这些放在重写规则上方的顶部附近。

关于apache - 通过 .htaccess 设置过期 header 和 gzipping 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13332581/

相关文章:

java - GemFire 9 替换 GemfireUtilLauncher

java - apache commons-fileupload 是否总是必须写入磁盘?

linux - Apache 重定向 (.htaccess)

php - .htaccess 子域的 url 重写

php - Localhost/phpmyadmin/返回php代码

apache - httpd.service 的作业失败,因为控制进程以错误代码退出。详见 "systemctl status httpd.service"和 "journalctl -xe"

php - 如何在 PHP (MAMP) 中启用错误报告?

php - 在本地计算机上使用 S3 类遇到 cURL 错误,但在主机上运行正常

linux - 使用 .htaccess 将任何 DomainName.com/FolderName 重定向到 DomainName.com

linux - 配置 Apache 以在访问和错误日​​志中记录主机