php 在 HTTP 标头中设置到期日期或最长期限
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20131039/
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
Setting an expiry date or a maximum age in the HTTP headers
提问by samayo
I just finished a website that I designated and submitted it to google insights
http://developers.google.com/speed/pagespeed/insights/for performance reviews, and this is the result I got. 
我刚刚完成了一个我指定的网站并将其提交给 google Insights
http://developers.google.com/speed/pagespeed/insights/进行性能评估,这就是我得到的结果。
It says, I need to set expiry date or a maximum age in the the HTTP headers, but I don't know how it is possible to set expiry date for anything other than cookies/sessions.
它说,我需要在 HTTP 标头中设置到期日期或最长期限,但我不知道如何为 cookie/会话以外的任何内容设置到期日期。
回答by Wayne
Generally that is done using the .htaccess file on your host. Here is an example cut and pasted from HTTP cache headers with .htaccess
通常这是使用主机上的 .htaccess 文件完成的。这是一个从HTTP 缓存标头中剪切和粘贴的示例,带有 .htaccess
<IfModule mod_headers.c>
# WEEK
<FilesMatch "\.(jpg|jpeg|png|gif|swf)$">
Header set Cache-Control "max-age=604800, public"
</FilesMatch>
</IfModule>
If delivering materials from a PHP shell you could use PHP to create the header in which case you would refer to the HTTP protocal outlined here section 14.9 Cache-Control http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html
如果从 PHP shell 提供材料,您可以使用 PHP 创建标头,在这种情况下,您可以参考此处概述的 HTTP 协议第 14.9 节缓存控制http://www.w3.org/Protocols/rfc2616/rfc2616-sec14。 html
<?php
/* This file is a wrapper, */
header( 'Cache-Control: max-age=604800' );
/* now get and send images */
?>
I consider the .htaccess the easier of the two methods.
我认为 .htaccess 是两种方法中更容易的。

