php 设置 HTTP 缓存过期,Google PageSpeed 推荐
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2676744/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Set HTTP Caching Expiration, Recommended by Google PageSpeed
提问by pws5068
I ran tests on my website using Google's PageSpeed and it recommends that I "Leverage browser caching" and provided the following resource:
我使用 Google 的 PageSpeed 在我的网站上进行了测试,它建议我“利用浏览器缓存”并提供以下资源:
http://code.google.com/speed/page-speed/docs/caching.html#LeverageBrowserCaching
http://code.google.com/speed/page-speed/docs/caching.html#LeverageBrowserCaching
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).
该资源从未解释如何实际更改我的 http 标头的到期日期。我是否通过 .htaccess 执行此操作?我想尽可能长时间地设置缓存(不违反谷歌最长一年的政策)。
Any advice on recommended settings (for a custom php-driven social networking community) would be greatly appreciated.
任何关于推荐设置的建议(对于自定义的 php 驱动的社交网络社区)将不胜感激。
回答by methode
In your root's .htaccess:
在您根目录的 .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>
And follow by:
并遵循:
<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>
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.
这是我在我管理的每个属性上使用的完全相同的代码,并为我(和 PageSpeed)提供了最令人满意的结果。人们可能会争论具体的规则,这就是为什么我说它满足我,但它肯定满足 PageSpeed。
回答by Mark
It can be done with both htaccess and php. Typically you wouldn't want to force caching the actual html since its dynamic database driven content (it can be done with the header()php function if needed). What you want to cache is external css & javascript, and image files.
它可以通过 htaccess 和 php 完成。通常,您不希望强制缓存实际的 html,因为它的动态数据库驱动内容(header()如果需要,可以使用php 函数完成)。您要缓存的是外部 css 和 javascript 以及图像文件。
See here for an .htaccess solution: http://www.askapache.com/htaccess/apache-speed-expires.html
有关 .htaccess 解决方案,请参见此处:http: //www.askapache.com/htaccess/apache-speed-expires.html

