【问题标题】:Can't wrap my head around Browser caching and gzip/deflate in .htaccess无法在 .htaccess 中处理浏览器缓存和 gzip/deflate
【发布时间】:2016-07-27 20:37:04
【问题描述】:

我已将这些行添加到我的.htaccess 文件中。是的,我了解他们应该做什么以及应该如何工作。我没有收到任何内部服务器错误,但 Google PageSpeed Insights 仍然说,我需要打开浏览器缓存并且我应该对文件进行压缩/gzip 压缩。这里有什么问题?

即使假设mod_filtermod_deflate 不可用,缓存控制至少应该可以工作,对吧?

.htaccess-文件:

# cache control
<filesMatch ".(css|jpg|jpeg|png|gif|js|ico)$">
    Header set Cache-Control "max-age=604800, public"
</filesMatch>

# gzip
<ifmodule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/text text/html text/plain text/xml text/css application/x-javascript application/javascript
</ifmodule>

# deflate
<IfModule mod_filter.c>
    AddOutputFilterByType DEFLATE "application/atom+xml" \
        "application/javascript" \
        "application/json" \
        "application/ld+json" \
        "application/manifest+json" \
        "application/rdf+xml" \
        "application/rss+xml" \
        "application/schema+json" \
        "application/vnd.geo+json" \
        "application/vnd.ms-fontobject" \
        "application/x-font-ttf" \
        "application/x-javascript" \
        "application/x-web-app-manifest+json" \
        "application/xhtml+xml" \
        "application/xml" \
        "font/eot" \
        "font/opentype" \
        "image/bmp" \
        "image/svg+xml" \
        "image/vnd.microsoft.icon" \
        "image/x-icon" \
        "text/cache-manifest" \
        "text/css" \
        "text/html" \
        "text/javascript" \
        "text/plain" \
        "text/vcard" \
        "text/vnd.rim.location.xloc" \
        "text/vtt" \
        "text/x-component" \
        "text/x-cross-domain-policy" \
        "text/xml"
</IfModule>

其他部分,如 mod_rewrite 正在像魅力一样工作。所以.htaccess被明确地考虑在内。也许我只是在这里遗漏了一些东西。

【问题讨论】:

  • 是否有任何文件类型被压缩?最好使用浏览器调试器的网络部分而不是 google pageinsights 来检查这一点。您可以单独检查页面的每个组件(css、js、html 等),以查看是否有正确的 Content-encoding: gzip 标头。
  • 我已经用 Firebug 对其进行了检查,它似乎可以正常工作 - Google PageSpeed Insights 现在也在检查它。可能是他们网站上的缓存问题。非常感谢您的提示。有趣的是,PageSpeed Insights 仍然表示应该改进 analytics.js 和 Maps API 的缓存。 :D

标签: html .htaccess gzip browser-cache deflate


【解决方案1】:

要启用缓存,请使用:

## EXPIRES CACHING ##
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access 1 year"
ExpiresByType image/jpeg "access 1 year"
ExpiresByType image/gif "access 1 year"
ExpiresByType image/png "access 1 year"
ExpiresByType text/css "access 1 month"
ExpiresByType application/pdf "access 1 month"
ExpiresByType text/x-javascript "access 1 month"
ExpiresByType application/x-shockwave-flash "access 1 month"
ExpiresByType image/x-icon "access 1 year"
ExpiresDefault "access 2 days"
</IfModule>

## EXPIRES CACHING ##

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-05-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-13
    • 1970-01-01
    • 1970-01-01
    • 2017-12-10
    相关资源
    最近更新 更多