htaccess Expires标头中的更改未反映在浏览器中 [英] Changes in htaccess Expires header not reflecting in browser
问题描述
我已将网站上的所有JS设置为缓存一周。但是,有些特定文件需要以较高的频率刷新。因此,我已使用FilesMatch在我的.htaccess文件中实现异常:
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>
例外文件位于 /sandboxassets/js/ga.js
和正在使用的 .htaccess 位于文档根目录( /
)。但是,浏览器似乎不接受此异常,并为相关文件提供了以下标头:
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秒是1周,这是我的 .htaccess 中定义的所有JS资产的默认值,但我需要为172800秒(2天)。我究竟做错了什么?另外,有什么方法可以告诉浏览器刷新到最新的标题吗? Opera返回33156000秒(一年),这是我最近将所有JS更改为1周的时间!
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!
更新:使用以下命令代码导致我的网站完全崩溃,并显示错误500:
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>
我首先在< IfModule mod_expires.c>
,然后紧随其后,每次都遇到相同的错误。
I added it first inside <IfModule mod_expires.c>
and then immediately after it and experienced the same error each time.
推荐答案
我的第一个倾向是,您需要能够在目录目标块内使用'AllowOverride'来实现特定文件的覆盖。
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>
来源:
http://www.websiteoptimization.com/secrets/advanced/caching-example.html
我强烈建议您在排除任何内容之前关闭CloudFlare进行测试。
I highly recommend turning off CloudFlare to do the testing of this before you rule anything out.
检查出回答此问题: https://webmasters.stackexchange.com/questions/9513/htaccess -执行顺序和优先级
我添加这个是因为我认为这也可能是一个订购问题。 .htaccess从根开始,并遍历目录。
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.
这篇关于htaccess Expires标头中的更改未反映在浏览器中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!