我正在我的网站上运行Google PagesPeed,这告诉我我需要
“指定缓存验证器。”

以下资源缺少缓存验证器。不能有效地刷新未指定缓存验证器的资源。指定最后修饰或ETAG标头以启用以下资源的缓存验证:

...然后列出图像,CSS,JS等。

根据 http://code.google.com/speed/page-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>

我究竟做错了什么?

有帮助吗?

解决方案

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

其他提示

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

Expires 或者 Cache-Control: max-age

Last-Modified 或者 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计算器。

我测试了上述所有代码,但是看到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>

我建议您自定义网站及其自己的文件。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top