设置HTTP过期使用PHP和Apache头 [英] Setup HTTP expires headers using PHP and Apache

查看:246
本文介绍了设置HTTP过期使用PHP和Apache头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎样才能设置在PHP + Apache的Expires头?我目前使用的AUTO_ prePEND服务gzip压缩的资源,但我也想最大限度的HTTP缓存。

How can I setup expires headers in PHP + Apache? I'm currently using an auto_prepend to serve resources gzipped but I'd also like to maximise the HTTP cache.

我如何设置这些?

推荐答案

有两种方法可以做到这一点。首先是指定你的php code头。如果你想以编程方式调整到期时间这是伟大的。例如维基可以设置一个更长的时间未编辑经常页面过期时间。

There are two ways to do this. The first is to specify the header in your php code. This is great if you want to programatically adjust the expiry time. For example a wiki could set a longer expires time for a page which is not edited very often.

header('Expires: '.gmdate('D, d M Y H:i:s \G\M\T', time() + (60 * 60))); // 1 hour

您的第二选择是创建一个的.htaccess 文件或修改你的httpd配置。在一个共享的托管环境,修改你的.htaccess文件是相当普遍的。为了做到这一点,你需要知道,如果你的服务器支持的指定mod_expires mod_headers中或两者兼而有之。最简单的方法就是试错,但一些的Apache服务器被配置为允许您查看通过/服务器信息页面信息。如果你的服务器有两个指定mod_expires和mod_headers中,并且要设置静态资源期满后,尝试把这个在你的.htaccess文件:

Your second choice is to create an .htaccess file or modify your httpd config. In a shared hosting environment, modifying your .htaccess file is quite common. In order to do this, you need to know if your server supports mod_expires, mod_headers or both. The easiest way is simply trial and error, but some Apache servers are configured to let you view this information via the /server-info page. If your server has both mod_expires and mod_headers, and you want to set the expiry on static resources, try putting this in your .htaccess file:

# Turn on Expires and set default to 0
ExpiresActive On
ExpiresDefault A0

# Set up caching on media files for 1 year (forever?)
<FilesMatch "\.(flv|ico|pdf|avi|mov|ppt|doc|mp3|wmv|wav)$">
ExpiresDefault A29030400
Header append Cache-Control "public"
</FilesMatch>

对于其他的组合和更多示例,请参阅:<一href=\"http://www.askapache.com/htaccess/speed-up-your-site-with-caching-and-cache-control.html\">http://www.askapache.com/htaccess/speed-up-your-site-with-caching-and-cache-control.html

这篇关于设置HTTP过期使用PHP和Apache头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆