问题描述
我跑了使用谷歌的PageSpeed在我的网站的测试,并建议我杠杆浏览器缓存,并提供以下资源:
I ran tests on my website using Google's PageSpeed and it recommends that I "Leverage browser caching" and provided the following resource:
<一个href="http://$c$c.google.com/speed/page-speed/docs/caching.html#LeverageBrowserCaching">http://$c$c.google.com/speed/page-speed/docs/caching.html#LeverageBrowserCaching
这个资源没有解释如何真正改变我的HTTP头文件的截止日期。做到这一点通过的.htaccess?我想设置缓存尽可能长的时间(不违反一年最大的谷歌的政策)。
This resource never explains how to actually change the expiration date of my http headers. Do I do this through .htaccess? I would like to set the caching for as long as possible (without violating Google's policy of a year max).
在推荐设置(为自定义PHP驱动的社交网络社区)的任何意见将是极大的AP preciated。
Any advice on recommended settings (for a custom php-driven social networking community) would be greatly appreciated.
推荐答案
在你的根目录的htaccess的:
In your root's .htaccess:
<IfModule mod_expires.c>
ExpiresActive On
ExpiresDefault "access plus 1 seconds"
ExpiresByType image/x-icon "access plus 2592000 seconds"
ExpiresByType image/jpeg "access plus 2592000 seconds"
ExpiresByType image/png "access plus 2592000 seconds"
ExpiresByType image/gif "access plus 2592000 seconds"
ExpiresByType application/x-shockwave-flash "access plus 2592000 seconds"
ExpiresByType text/css "access plus 604800 seconds"
ExpiresByType text/javascript "access plus 216000 seconds"
ExpiresByType application/x-javascript "access plus 216000 seconds"
ExpiresByType text/html "access plus 600 seconds"
ExpiresByType application/xhtml+xml "access plus 600 seconds"
</IfModule>
和遵循:
<IfModule mod_headers.c>
<FilesMatch "\\.(ico|jpe?g|png|gif|swf)$">
Header set Cache-Control "max-age=2692000, public"
</FilesMatch>
<FilesMatch "\\.(css)$">
Header set Cache-Control "max-age=2692000, public"
</FilesMatch>
<FilesMatch "\\.(js)$">
Header set Cache-Control "max-age=216000, private"
</FilesMatch>
<FilesMatch "\\.(x?html?|php)$">
Header set Cache-Control "max-age=600, private, must-revalidate"
</FilesMatch>
Header unset ETag
Header unset Last-Modified
</IfModule>
这是完全相同的code我对每个属性我管理,并提供我(和PageSpeed)最满意的效果,请使用。有人可能会说在特定的规则,这就是为什么我说,它满足的我,但它肯定满足PageSpeed。
This is the exact same code I use on every property I manage and offers me (and PageSpeed) the most satisfying results. One may argue on specific rules, that's why I said that it satisfies me, but it certainly satisfies PageSpeed.
这篇关于设置HTTP缓存过期,推荐由谷歌PageSpeed的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!