.htaccess:如何“指定缓存验证器”?
我在网站上运行 Google PageSpeed,它告诉我需要
“指定一个缓存验证器。”
以下资源缺少缓存验证器。未指定缓存验证器的资源无法有效刷新。指定 Last-Modified 或 ETag 标头以启用以下资源的缓存验证:
...然后它列出图像、CSS、JS 等。
根据 http://code.google.com/speed/page-speed/docs/caching.html#LeverageBrowserCaching:
将“上次修改日期”设置为上次更改资源的时间。如果上次修改日期距离过去足够远,浏览器很可能不会重新获取它。
我的 .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>
我做错了什么?
I'm running Google PageSpeed on my site and it's tell me that I need to
"Specify a cache validator."
The following resources are missing a cache validator. Resources that do not specify a cache validator cannot be refreshed efficiently. Specify a Last-Modified or ETag header to enable cache validation for the following resources:
... then it lists images, CSS, JS, etc.
According to http://code.google.com/speed/page-speed/docs/caching.html#LeverageBrowserCaching:
Set the Last-Modified date to the last time the resource was changed. If the Last-Modified date is sufficiently far enough in the past, chances are the browser won't refetch it.
I have the following in my .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>
What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为您遇到的问题是
Expire:
而不是Last-Modified:
。 Apache 默认情况下会根据文件日期发送文件Last-Modified:
标头。我建议删除上面的代码并将其替换为以下代码:尝试一下,如果它不起作用,也尝试添加以下代码:
I think the problem you are having is with
Expire:
and not withLast-Modified:
. Apache would by default send the fileLast-Modified:
header based on the file date. I suggest removing the upper code and replacing it with the following:Try with that, if it didn't work try adding this as well:
为了“设置缓存验证器”,您需要在标头中发送以下内容:
Expires
或Cache-Control: max-age
< em>AND
Last-Modified
orETag
因此,例如,在 PHP 中,您可以为 CSS 和 JS 文件添加以下内容:
这将满足 Google 的 Pagespeed 计算器的要求。
In order to "Set A Cache Validator" you need to send the following in your headers:
Expires
orCache-Control: max-age
AND
Last-Modified
orETag
So, for example, in PHP you could add the following for CSS and JS files:
This will satisfy Google's Pagespeed calculator.
我测试了以上所有代码,但没有看到 gtmetrix 排名发生变化。
对我的 WordPress 站点使用此改进的缓存控制(指定缓存验证器)排名:
我建议您自己为站点及其文件自定义 max-age 值。
I tested all of above codes, But see no change in gtmetrix rank.
Using this improved Cache-Control (Specify a cache validator) rank for my wordpress site:
I recommend you to customize max-age values for your site and it's files yourself.