【问题标题】:Leverage Browser Caching in .htaccess not working利用 .htaccess 中的浏览器缓存不起作用
【发布时间】:2014-12-24 00:22:08
【问题描述】:

我的 .htaccess 文件中有这个。

<IfModule mod_expires.c>

    ExpiresActive on
    ExpiresDefault                                      "access plus 1 month"

  # CSS
    ExpiresByType text/css                              "access plus 1 month”

  # Data interchange
    ExpiresByType application/atom+xml                  "access plus 1 hour"
    ExpiresByType application/rdf+xml                   "access plus 1 hour"
    ExpiresByType application/rss+xml                   "access plus 1 hour"

    ExpiresByType application/json                      "access plus 0 seconds"
    ExpiresByType application/ld+json                   "access plus 0 seconds"
    ExpiresByType application/schema+json               "access plus 0 seconds"
    ExpiresByType application/vnd.geo+json              "access plus 0 seconds"
    ExpiresByType application/xml                       "access plus 0 seconds"
    ExpiresByType text/xml                              "access plus 0 seconds"

  # Favicon (cannot be renamed!) and cursor images
    ExpiresByType image/vnd.microsoft.icon              "access plus 1 week"
    ExpiresByType image/x-icon                          "access plus 1 week"

  # HTML
    ExpiresByType text/html                             "access plus 2 day”

  # JavaScript
    ExpiresByType application/javascript                "access plus 1 month”
    ExpiresByType application/x-javascript              "access plus 1 year"
    ExpiresByType text/javascript                       "access plus 1 year"

  # Manifest files
    ExpiresByType application/manifest+json             "access plus 1 year"

    ExpiresByType application/x-web-app-manifest+json   "access plus 0 seconds"
    ExpiresByType text/cache-manifest                   "access plus 0 seconds"

  # Media files
    ExpiresByType audio/ogg                             "access plus 1 month"
    ExpiresByType image/bmp                             "access plus 1 month"
    ExpiresByType image/gif                             "access plus 1 month"
    ExpiresByType image/jpeg                            "access plus 1 month"
    ExpiresByType image/png                             "access plus 1 month"
    ExpiresByType image/svg+xml                         "access plus 1 month"
    ExpiresByType video/mp4                             "access plus 1 month"
    ExpiresByType video/ogg                             "access plus 1 month"
    ExpiresByType video/webm                            "access plus 1 month"

  # Web fonts

    # Embedded OpenType (EOT)
    ExpiresByType application/vnd.ms-fontobject         "access plus 1 month"
    ExpiresByType font/eot                              "access plus 1 month"

    # OpenType
    ExpiresByType font/opentype                         "access plus 1 month"

    # TrueType
    ExpiresByType application/x-font-ttf                "access plus 1 month"

    # Web Open Font Format (WOFF) 1.0
    ExpiresByType application/font-woff                 "access plus 1 month"
    ExpiresByType application/x-font-woff               "access plus 1 month"
    ExpiresByType font/woff                             "access plus 1 month"

    # Web Open Font Format (WOFF) 2.0
    ExpiresByType application/font-woff2                "access plus 1 month"

  # Other
    ExpiresByType text/x-cross-domain-policy            "access plus 1 week"

</IfModule>

但在 gtmetrix 上它仍然说: The following cacheable resources have a short freshness lifetime. Specify an expiration at least one week in the future for the following resources

【问题讨论】:

  • 以下资源是什么?只是CSS,一切?当您在浏览器中打开页面并打开开发人员工具的网络选项卡时,文件响应的标题部分会显示什么内容?
  • gtmetrix 会说哪些文件没有被缓存? Google Page Speed 列出了没有缓存/缓存太少的文件。
  • 尝试了谷歌页面速度,它给出了相同的结果。利用浏览器缓存处理 jpg、css、js 文件
  • 至少我们现在知道哪些文件类型没有被缓存。尝试在ExpiresByType image/jpeg 之前添加AddType image/jpeg .jpg 并再次检查jpg 文件。
  • 文件是否托管在您的服务器上,或者这些文件是否被任何第三方服务(如 Facebook 插件、Twitter Widget 等)使用?您不能为其他服务器上的资源设置任何缓存

标签: apache .htaccess


【解决方案1】:

您的 htaccess 文件的语法是正确的。请确定你

  • 将 htaccess 文件放在正确的文件夹中(即项目的根文件夹)
  • 安装并启用模块 mod_expires(参见how to check if mod_expires is enabled
  • 已为您的所有资源设置了所需的过期时间(哪些资源由“以下资源”管理?您已将某些资源的过期标头设置为最长一年)

【讨论】:

  • 感谢您的快速回复,我在终端尝试了这个命令:apachectl -M mime 和 headers 都是共享的,在任何地方都看不到 expires 模块
  • 在模块中编译:core.c mod_so.c http_core.c prefork.c
  • 如果您的列表中没有 expires_module,则说明它没有安装/启用。请您的提供商安装/启用它。而且,正如 Reeno 所说,您必须将 .htaccess 文件放在您的域指向的文件夹中。
猜你喜欢
  • 2017-01-20
  • 2013-01-21
  • 2016-09-18
  • 2016-01-13
  • 2014-11-03
  • 2017-01-09
  • 1970-01-01
相关资源
最近更新 更多