如果应用程序未设置,请添加Apache标头


Add Apache header if not set by application

我正在尝试控制某个目录中文件的缓存。我希望默认缓存时间为15分钟,但如果需要,我希望让应用程序更改它。例如,我可能有一个PHP脚本,我想每1分钟刷新一次,所以我将在PHP中为该脚本设置缓存控制头。但对于所有其他文件,我只希望缓存时间为15分钟,其中一些是静态文件,所以我不能在PHP中设置默认的缓存时间。

我目前在我的Apache配置中有这个:

<Directory />
    Options FollowSymLinks
    AllowOverride None
    Header set Cache-Control "max-age=900"
</Directory>

这适用于99%的情况,我只需要15分钟的缓存。但是,如果我的PHP脚本设置了一个缓存控制头,那么这个设置将覆盖它

我看了mod_header的文档,没有一个设置(unset、add、append等)能满足我的需要。

提前谢谢。

改为查看mod_expireshttp://httpd.apache.org/docs/2.2/mod/mod_expires.html.文档说它不会覆盖PHP脚本创建的头:

"当Expires标头已经是由服务器,例如由CGI脚本生成或代理时源服务器,此模块不会更改或添加Expires或缓存控制标头。"

以下是mod_expires的配置示例:

 <IfModule mod_expires.c>
     ExpiresActive on
     ExpiresDefault A600
     ExpiresByType image/gif "access plus 1 day"
     ExpiresByType image/jpeg "access plus 1 day"
     ExpiresByType image/png "access plus 1 day"
     ExpiresByType image/x-icon "access plus 1 day"
     <FilesMatch "'.(php|php4)$">
         ExpiresByType text/html "now"
     </FilesMatch> 
 </IfModule>

取自http://howto.gumph.org/content/reduce-webserver-bandwidth/

祝你好运!

根据php手册

<?php
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
?>

通过发送上面的头,您应该覆盖任何可能导致脚本输出被缓存的设置。