2011-12-15 98 views
8

我将以下内容输入到我的“.htacces”文件中,以便开始缓存Web内容。根据Google Page Speed和Yslow的说法,页面仍然没有被缓存。模块是否错误?还是应用程序没有正确显示数据?htaccess缓存不起作用

网站上的Apache 2.0运行的服务器上

的.htaccess(带缓存模块部分):

# Expire headers 
<ifModule mod_expires.c> 
  ExpiresActive On 
  ExpiresDefault "access plus 1 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> 
  
# Cache-Control Headers 
<ifModule mod_headers.c> 
#month 
  <filesMatch "\.(ico|jpe?g|png|gif|swf)$"> 
    Header set Cache-Control "max-age=2592000, public" 
  </filesMatch> 
#week 
  <filesMatch "\.(css|js)$"> 
    Header set Cache-Control "max-age=604800, public" 
  </filesMatch> 
#day 
  <filesMatch "\.(x?html?|php)$"> 
    Header set Cache-Control "max-age=43200, private, must-revalidate" 
  </filesMatch> 
</ifModule> 
# END Cache-Control Headers 
  
# Turn ETags Off 
<ifModule mod_headers.c> 
  Header unset ETag 
</ifModule> 
FileETag None 
  
# Remove Last-Modified Header 
<ifModule mod_headers.c> 
  Header unset Last-Modified 
</ifModule> 
+0

检查发送的Expires报头。使用Chrome开发者工具或萤火虫中的网路标签。可能是因为htaccess被禁用,或者mod_expires未启用。 – Gerben 2011-12-15 16:43:38

回答

19

走进httpd.conf并查找mod_expires线,它不应该被注释掉。查找mod_headers行,并确保它未被注释掉。

或(不是一个关键的应用程序)有一个简单的和肮脏的测试:删除<ifModule mod_expires.c></ifModule>假的东西之间,同样适用于<ifModule mod_headers.c>,如果你的服务器失败,500互联网服务器错误,那么你很可能缺少一个或两个这些模块并没有启用。如果是这样,那么进入httpd.conf并启用你需要的。

您还可以使用类似REDbot这样的工具来测试您网站的回复标题。只需选择一个资源URL(如指向图像的URL)并将其粘贴到工具中,即可查看哪些标头会返回并附带一些建议。请注意,它遵循域的robots.txt规则,并且不会检查资源是否被禁止。

就像Gerben所说的那样,使用firefox中的net选项卡,chrome开发工具或一些等效的Web开发工具可以帮助您查看哪些头部正在发送和接收。您也不需要设置Cache-Control public。如果您还使用ExpiresByType来电,则不需要使用max age

欲了解更多信息阅读这个伟大的教程:http://www.mnot.net/cache_docs/

而且学习的榜样:结账它是如何在html5-boilerplatehttps://github.com/h5bp/html5-boilerplate/blob/master/dist/.htaccess

做对于其他流行的服务器配置实例像lighthttpdNode.jsNginx等。参见: https://github.com/h5bp/server-configs

+0

@Anthony链接到这篇文章底部的html5样板文件相当有帮助。例如,我正在写`jpg`而不是`jpeg`。感谢那! – 2014-07-09 11:18:05