htaccess中的更改过期标头未反映在浏览器

时间:2017-03-02 15:19:16

标签: .htaccess mod-expires expires-header

我已将我网站上的所有JS设置为缓存一周。但是,我需要以更高的频率刷新特定文件。因此我使用FilesMatch来实现我的.htaccess文件中的异常:

<IfModule mod_expires.c>

    ExpiresActive on
    ExpiresDefault                                      "access plus 1 month"

  # CSS
    ExpiresByType text/css                              "access plus 1 year"

  # Data interchange
    ExpiresByType application/json                      "access plus 0 seconds"
    ExpiresByType application/xml                       "access plus 0 seconds"
    ExpiresByType text/xml                              "access plus 0 seconds"

  # Favicon (cannot be renamed!)
    ExpiresByType image/x-icon                          "access plus 1 week"

  # HTML components (HTCs)
    ExpiresByType text/x-component                      "access plus 1 month"

  # HTML
    ExpiresByType text/html                             "access plus 0 seconds"

  # JavaScript
    ExpiresByType text/javascript                       "access plus 1 week"
    ExpiresByType application/javascript                "access plus 1 week"
    ExpiresByType application/x-javascript              "access plus 1 week"

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

  # Media
    ExpiresByType audio/ogg                             "access plus 1 month"
    ExpiresByType image/gif                             "access plus 1 month"
    ExpiresByType image/jpeg                            "access plus 1 month"
    ExpiresByType image/jpg                            "access plus 1 month"
    ExpiresByType image/png                             "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 feeds
    ExpiresByType application/atom+xml                  "access plus 1 hour"
    ExpiresByType application/rss+xml                   "access plus 1 hour"

  # Web fonts
    ExpiresByType application/font-woff                 "access plus 1 month"
    ExpiresByType application/vnd.ms-fontobject         "access plus 1 month"
    ExpiresByType application/x-font-ttf                "access plus 1 month"
    ExpiresByType font/opentype                         "access plus 1 month"
    ExpiresByType image/svg+xml                         "access plus 1 month"

</IfModule>
<FilesMatch "^(ga\.js)$">
  ExpiresActive on 
  ExpiresDefault "access plus 2 days"
</FilesMatch>

异常文件位于/sandboxassets/js/ga.js,我正在使用的 .htaccess 位于文档根目录(/)。但是,浏览器似乎没有遵守此异常,并为相关文件提供以下标题:

Accept-Ranges:bytes
Access-Control-Allow-Origin:*
Age:0
Cache-Control:max-age=604800
Content-Encoding:gzip
Content-Length:11967
Content-Type:application/javascript
Date:Thu, 02 Mar 2017 15:03:55 GMT
Expires:Thu, 09 Mar 2017 15:03:55 GMT
Last-Modified:Thu, 02 Mar 2017 15:00:01 GMT
Server:Apache Phusion_Passenger/4.0.10 mod_bwlimited/1.4 mod_fcgid/2.3.9
Vary:Accept-Encoding,User-Agent
Via:1.1 varnish-v4
X-Varnish:18808880

604800秒是1周,这是我的 .htaccess 中定义的所有JS资产的默认值,但我需要它是172800秒(2天)。我究竟做错了什么?另外,有没有办法告诉浏览器刷新到最新的标题? Opera将返回31536000秒(1年),这是我最近改为所有JS的1周!

更新:使用以下代码导致我的网站完全崩溃并出现错误500:

<Directory "/sandboxassets/js/">
    AllowOverride All
    <FilesMatch "ga\.js$">
        Expires A172800
    </FilesMatch>
    <FilesMatch "tww\.js$">
        Expires A172800
    </FilesMatch>
</Directory>

我首先在<IfModule mod_expires.c>内添加它,然后立即在它之后添加它并且每次都遇到相同的错误。

1 个答案:

答案 0 :(得分:0)

我的第一个倾向是你需要能够使用&#39; AllowOverride&#39;在目标目标块中实现特定文件的覆盖。

<Directory "/directory/to/your/file">
    AllowOverride All
    <FilesMatch "\.js$">
        Expires A31536000
    </FilesMatch>
</Directory>

来源(S): http://www.websiteoptimization.com/secrets/advanced/caching-example.html

我强烈建议在你排除任何内容之前关闭CloudFlare进行测试。

查看此问题的答案可能也是有益的:https://webmasters.stackexchange.com/questions/9513/htaccess-execution-order-and-priority

我添加这个是因为我认为这也是一个排序问题。 .htaccess从根开始,并在目录中运行。