.htaccess: 如何“指定缓存验证器”?

23

我正在运行Google PageSpeed对我的网站进行检查,它告诉我需要“指定缓存验证器”。

以下资源缺少缓存验证器。未指定缓存验证器的资源无法有效地刷新。请指定Last-Modified或ETag标头,以启用以下资源的缓存验证:

...然后列出了图片、CSS、JS等。

根据http://code.google.com/speed/page-speed/docs/caching.html#LeverageBrowserCaching:

将Last-Modified日期设置为资源最后更改的时间。如果Last-Modified日期足够久远,浏览器可能不会重新获取它。

我在我的.htaccess文件中添加了以下内容:

<IfModule mod_headers.c>
    <FilesMatch "\.(bmp|css|flv|gif|ico|jpg|jpeg|js|pdf|png|svg|swf|tif|tiff)$">
        Header set Last-Modified "Tue, 31 Aug 2010 00:00:00 GMT"
    </FilesMatch>
</IfModule>

我做错了什么?

3个回答

16

我认为你遇到的问题与Expire:有关,而不是与Last-Modified:有关。Apache默认会基于文件日期发送文件的Last-Modified:标头。我建议移除上面的代码,并将其替换为以下内容:

<IfModule mod_expires.c>
    ExpiresActive On
    ExpiresDefault "access plus 1 year"
</IfModule>

如果那个不起作用,尝试加上这个:

<IfModule mod_headers.c>
    <FilesMatch "\.(bmp|css|flv|gif|ico|jpg|jpeg|js|pdf|png|svg|swf|tif|tiff)$">
        Header set Last-Modified "Mon, 31 Aug 2009 00:00:00 GMT"
    </FilesMatch>
</IfModule>

我已经在mod_expires中设置了像ExpiresByType text/css "access plus 1 year"这样的内容。还有其他建议吗? - StackOverflowNewbie
尝试使用新的Header set Last-Modified "Mon, 31 Aug 2009 00:00:00 GMT"代替您现有的,这个已经过时了一年! - aularon
尝试过了,没有任何改变。还有其他的想法吗? - StackOverflowNewbie
很遗憾,唯一更好的想法是检查谷歌的页面速度代码,并检查它在响应中具体寻找什么,而不是盲目尝试,因为他们的文档只是一般性建议,没有具体规则。 - aularon
2
感谢这篇有用的文章,它通过htaccess立即解决了我的问题。页面速度列出问题后,你必须研究如何解决它们,这真的很烦人。对于非技术人员来说,这可能会证明是困难的。 - user777634
为了完整性:还要注意ETag是可能的缓存验证器。为此,可以使用Apache中的FileETag指令 - user824425

6
为了“设置缓存验证器”,您需要在头部发送以下内容: ExpiresCache-Control: max-ageLast-ModifiedETag 例如,在PHP中,您可以为CSS和JS文件添加以下内容:
<filesMatch "\.(js|css)$">
    Header set Expires "Thu, 21 May 2013 20:00:00 GMT"
    Header set Last-Modified "Thu, 21 May 2012 20:00:00 GMT"
</filesMatch>

这将满足Google的页面速度计算器要求。

2
我测试了以上所有代码,但在gtmetrix排名中没有看到任何变化。 使用这个改进的Cache-Control(指定缓存验证器)可以提高我的WordPress网站的排名:
## EXPIRES CACHING ##
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access 1 year"
ExpiresByType image/jpeg "access 1 year"
ExpiresByType image/gif "access 1 year"
ExpiresByType image/png "access 1 year"
ExpiresByType text/css "access 1 month"
ExpiresByType application/pdf "access 1 month"
ExpiresByType text/x-javascript "access 1 month"
ExpiresByType application/x-shockwave-flash "access 1 month"
ExpiresByType image/x-icon "access 1 year"
ExpiresDefault "access plus 1 year"
</IfModule>
## EXPIRES CACHING ##

<ifModule mod_headers.c>
  <filesMatch "\\.(ico|pdf|flv|jpg|jpeg|png|gif|swf)$">
    Header set Cache-Control "max-age=2592000, public"
  </filesMatch>

  <filesMatch "\\.(css)$">
    Header set Cache-Control "max-age=2592000, public"
  </filesMatch>

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

  <filesMatch "\\.(xml|txt)$">
    Header set Cache-Control "max-age=216000, public, must-revalidate"
  </filesMatch>

  <filesMatch "\\.(html|htm|php)$">
    Header set Cache-Control "max-age=1, private, must-revalidate"
  </filesMatch>
</ifModule>

我建议您自行定制站点和文件的max-age值。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接