Changes in htaccess Expires header not reflecting in browser

半腔热情 提交于 2019-12-10 12:22:38

问题


I have set all JS on my site to cache for one week. However, there are specific files that I need to be refreshed at a higher frequency. Hence I have used FilesMatch to effect the exception in my .htaccess file:

<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>

The exception file is at /sandboxassets/js/ga.js and the .htaccess I am working with is at the document root (/). However, the browser doesn't seem to honor this exception and gives the following header for the file in question:

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 seconds is 1 week which is the default for all JS assets as defined in my .htaccess but I need it to be 172800 seconds (2 days). What am I doing wrong? Also, is there any way to tell the browser to refresh to the latest header? Opera is returning a 31536000 seconds (1 year) which is what I recently changed to 1 week for all JS!

Update: Using the following code is causing my site to break down completely with an error 500:

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

I added it first inside <IfModule mod_expires.c> and then immediately after it and experienced the same error each time.


回答1:


My first inclination is that you need to be able to use the 'AllowOverride' within a Directory target block to achieve the override of a specific file.

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

Source(s): http://www.websiteoptimization.com/secrets/advanced/caching-example.html

I highly recommend turning off CloudFlare to do the testing of this before you rule anything out.

It may also be beneficial to check out the answer to this question: https://webmasters.stackexchange.com/questions/9513/htaccess-execution-order-and-priority

I am adding this because I think that it is also possible that this is an ordering issue. .htaccess starts at the root and works its way through the directories.



来源:https://stackoverflow.com/questions/42559235/changes-in-htaccess-expires-header-not-reflecting-in-browser

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!