htaccess for Leverage浏览器缓存不起作用

时间:2013-02-06 12:04:36

标签: .htaccess http caching http-headers browser-cache

我遇到Leverage浏览器缓存问题。 我创建了这个.htaccess文件并在我的服务器上使用它,但它似乎没有工作,我从这里复制了这个" http://www.samaxes.com/2011/05/improving-web-performance-with-apache-and-htaccess/"。 我最喜欢犯一个基本错误,所以任何帮助都会很好。 这里有一些细节。我正在研究一个子域名,让我们称之为" sub"和主要领域"示例"。 所以我希望http://sub.example.com/中的所有内容都能用于htaccess文件。 在我的服务器上,我将.htaccess文件放在子主目录中,例如与主要目录分开。

我正在运行一个apache2服务器,同时启用了mod_headers和mod_expires。

干杯。

<ifModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/html text/xml text/css text/plain
AddOutputFilterByType DEFLATE image/svg+xml application/xhtml+xml application/xml
AddOutputFilterByType DEFLATE application/rdf+xml application/rss+xml application/atom+xml
AddOutputFilterByType DEFLATE text/javascript application/javascript application/x-javascript application/json
AddOutputFilterByType DEFLATE application/x-font-ttf application/x-font-otf
AddOutputFilterByType DEFLATE font/truetype font/opentype
</ifModule>
<ifModule mod_expires.c>
 ExpiresActive On
 ExpiresDefault "access plus 5 seconds"
 ExpiresByType image/x-icon "access plus 2592000 seconds"
 ExpiresByType image/jpeg "access plus 2592000 seconds"
 ExpiresByType image/png "access plus 2592000 seconds"
 ExpiresByType image/gif "access plus 2592000 seconds"
 ExpiresByType application/x-shockwave-flash "access plus 2592000 seconds"
 ExpiresByType text/css "access plus 604800 seconds"
 ExpiresByType text/javascript "access plus 216000 seconds"
 ExpiresByType application/javascript "access plus 216000 seconds"
 ExpiresByType application/x-javascript "access plus 216000 seconds"
 ExpiresByType text/html "access plus 600 seconds"
 ExpiresByType application/xhtml+xml "access plus 600 seconds"
 </ifModule>
 <filesMatch "\\.(ico|pdf|flv|jpg|jpeg|png|gif|swf|mp3|mp4)$">
 Header set Cache-Control "max-age=2592000, private"
 Header set Expires "Sun, 17 July 2013 20:00:00 GMT"
 </filesMatch>
 <filesMatch "\\.(css|css.gz)$">
 Header set Cache-Control "max-age=604800, private" 
 </filesMatch>
 <filesMatch "\\.(js|js.gz)$">
 Header set Cache-Control "max-age=604800, private"
 </filesMatch>
 <filesMatch "\\.(xml|txt)$">
 Header set Cache-Control "max-age=216000, private, must-revalidate"
 </filesMatch>
 <filesMatch "\\.(html|htm)$">
 Header set Cache-Control "max-age=7200, private, must-revalidate"
 </filesMatch>
 FileETag None

2 个答案:

答案 0 :(得分:6)

看起来非常凌乱。

您还有像

这样的冗余规则

ExpiresByType application/javascript "access plus 216000 seconds"

<filesMatch "\\.(js|js.gz)$">
Header set Cache-Control "max-age=604800, private"
</filesMatch>

尝试使用html5-boilerplate中的清洁(和最新)规则或全部结帐 h5bp server configs

答案 1 :(得分:0)

只使用这个并试一试:

    <ifModule mod_expires.c>
 ExpiresActive On
 ExpiresDefault "access plus 5 seconds"
 ExpiresByType image/x-icon "access plus 2592000 seconds"
 ExpiresByType image/jpeg "access plus 2592000 seconds"
 ExpiresByType image/png "access plus 2592000 seconds"
 ExpiresByType image/gif "access plus 2592000 seconds"
 ExpiresByType application/x-shockwave-flash "access plus 2592000 seconds"
 ExpiresByType text/css "access plus 604800 seconds"
 ExpiresByType text/javascript "access plus 216000 seconds"
 ExpiresByType application/javascript "access plus 216000 seconds"
 ExpiresByType application/x-javascript "access plus 216000 seconds"
 ExpiresByType text/html "access plus 600 seconds"
 ExpiresByType application/xhtml+xml "access plus 600 seconds"
 </ifModule>

就像@Anthony Hatzopoulos指出的那样,您使用的是重复配置,它们可能存在冲突。当然,如果你喜欢你也可以使用“fileMatch”方式,但问题是它们不是很好的代码。请注意,如果您使用“fileMatch”,则不使用该代码。