Skip to main content
 首页 » 编程设计

performance之.htaccess:如何 “Specify a cache validator”

2025年01月19日57shanyou

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

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

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

根据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.



我的.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: header 。我建议删除上面的代码并将其替换为以下代码:

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