有条件地在Apache配置中设置expires头

时间:2011-06-16 14:35:50

标签: apache mod-rewrite http-headers apache-config mod-expires

我想有条件地在图像上设置过期标题,这样它们就不会在项目处于开发状态时进行缓存,而是在生产时进行缓存。理想情况下,这只是对apache conf文件的修改。我有一个perl脚本,它将返回项目的状态,可以与mod_rewrite一起使用,如下所示:

rewritemap  PSTAT prg:/bin/pstat.pl
...skipping...
rewritecond ${PSTAT:$site:$1} =devel
rewriterule ^/run/$site/p(\d+)/(\w+) /logout.pl/$2 [NS,L]

如果我可以做的事情会很好:

rewritecond ${PSTAT:$site:$1} =devel
ExpiresByType image/jpg "now plus 1 second"

虽然当然不行。

有没有解决方案?

2 个答案:

答案 0 :(得分:1)

对我有用的技巧是首先无条件地设置标题:

ExpiresByType image/jpg "now plus 1 second"
...

然后在我们处于开发模式时取消设置标题:

Header set Cache-control "no-cache" env=devel
Header unset expires env=devel

这要求您先前根据您的模式初始化布尔env devel。在我们的案例中,我们决定主机名是否要开发(devel.domain.com与www.domain.com)。

答案 1 :(得分:0)

从他的说法开始,我设法找到了解决问题的方法。

rewritemap  PSTAT prg:/bin/pstat.pl
...skipping...
rewritecond ${PSTAT:$site:$1} =devel
rewriterule ^/images/(\d+)/(\w+) - [E=devel:1]

header set cache-control "no-cache" env=devel
header unset expires env=devel

/images/(\d+)是特定项目编号(\d+)

的图像文件夹

E的{​​{1}}标志允许您在规则匹配的情况下设置环境变量。 rewriterule实际上并没有重写任何内容。因此,它使用-rewritecond向其发送项目编号来检查脚本的输出,然后在两个条件匹配的情况下设置环境变量。然后根据该环境变量的存在有条件地设置rewriterule