Last-Modified不适用于.htaccess

Sta*_*bie 3 apache .htaccess header httpd.conf http-headers

我正在实施浏览器缓存,并遵循Google PageSpeed关于将Last-Modified设置为"过去足够远"的数据的建议.我的.htaccess中有以下内容:

<IfModule mod_headers.c>
 <FilesMatch "\.(json|pdf|swf|bmp|gif|jpeg|jpg|png|svg|tiff|ico|flv|js)$">
  Header Set Last-Modified "Fri, 01 Jan 2010 12:00:00 GMT"
 </FilesMatch>
</IfModule>
Run Code Online (Sandbox Code Playgroud)

我的服务器上安装了mod_headers.

不幸的是,Google PageSpeed仍抱怨并警告我:

Leverage browser caching

The following cacheable resources have a short freshness lifetime. Specify an expiration at least one week in the future for the following resources:
Run Code Online (Sandbox Code Playgroud)

然后列出PNG,GIF,JPG等.雅虎YSlow说的基本相同.

查看我应该缓存的一个资源的响应头,我看到了:

Date:           Tue, 19 Oct 2010 20:12:04 GMT
Server:         Apache/2.2.14 (Ubuntu)
Last-Modified:  Tue, 07 Sep 2010 23:51:33 GMT
Etag:           "2e0e34-2a43-48fb413a96a20"
Accept-Ranges:  bytes
Content-Length: 10819
Content-Type:   image/png
Run Code Online (Sandbox Code Playgroud)

如您所见,Last-Modified数据与我在.htaccess中指定的数据不匹配.

我有什么想法我做错了吗?

Mar*_*aio 7

删除Last-Modified不是Google PageSpeed所要求的.当浏览器要求提供静态文件时,它希望在服务器响应中看到以下标头:

Cache-Control max-age=...
Expires ...
Run Code Online (Sandbox Code Playgroud)

代替点,服务器将放置值.

为此,您只需添加.htaccess以下行:

<IfModule mod_headers.c>
 <FilesMatch "\.(json|pdf|swf|bmp|gif|jpeg|jpg|png|svg|tiff|ico|flv|js)$">
  ExpiresActive On
  ExpiresDefault "access plus 1 year"
  Header append Cache-Control "public"
 </FilesMatch>
</IfModule>
Run Code Online (Sandbox Code Playgroud)

您会看到Google PageSpeed停止投诉.