作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我很想实现浏览器缓存并遵循 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>
我的服务器上安装了 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:
然后列出 PNG、GIF、JPG 等。Yahoo YSlow 说的基本相同。
查看我应该缓存的资源之一的响应 header ,我看到了:
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
如您所见,Last-Modified 数据与我在 .htaccess 中指定的不匹配。
任何想法我做错了什么?
最佳答案
删除 Last-Modified
不是 Google PageSpeed 要求的。当浏览器请求静态文件时,它希望在您的服务器响应中看到以下 header :
Cache-Control max-age=...
Expires ...
服务器将放置值来代替点。
为此,您只需在 .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>
您会看到 Google PageSpeed 停止提示。
关于apache - Last-Modified 不适用于 .htaccess,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3982654/
我是一名优秀的程序员,十分优秀!