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

发布于 2024-09-17 08:11:52 字数 755 浏览 3 评论 0原文

我在网站上运行 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

满地尘埃落定 2024-09-24 08:11:52

我认为您遇到的问题是 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>

I think the problem you are having is with Expire: and not with Last-Modified:. Apache would by default send the file Last-Modified: header based on the file date. I suggest removing the upper code and replacing it with the following:

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

Try with that, if it didn't work try adding this as well:

<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>
病毒体 2024-09-24 08:11:52

为了“设置缓存验证器”,您需要在标头中发送以下内容:

Expires Cache-Control: max-age

< em>AND

Last-Modified or ETag

因此,例如,在 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 的 Pagespeed 计算器的要求。

In order to "Set A Cache Validator" you need to send the following in your headers:

Expires or Cache-Control: max-age

AND

Last-Modified or ETag

So, for example, in PHP you could add the following for CSS and JS files:

<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>

This will satisfy Google's Pagespeed calculator.

夜司空 2024-09-24 08:11:52

我测试了以上所有代码,但没有看到 gtmetrix 排名发生变化。
对我的 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 值。

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:

## 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>

I recommend you to customize max-age values for your site and it's files yourself.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文