简体   繁体   English

在Apache配置中有条件地设置到期标头

[英]Conditionally set expires headers in Apache config

I would like to conditionally set expires headers on images so that they will not cache while a project is in development but will when it is in production. 我想在图像上有条件地设置到期标头,以使它们在项目开发时不会缓存,而在生产时会缓存。 Ideally this would just be a modification of the apache conf file. 理想情况下,这只是对apache conf文件的修改。 I have a perl script that will return the status of the project, which can be used with mod_rewrite as follows: 我有一个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]

It would be nice if I could do something like: 如果能做类似的事情,那就太好了:

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

Though of course that wouldn't work. 虽然那当然是行不通的。

Is there any solution? 有什么解决办法吗?

A trick that worked for me is to first set the headers unconditionally: 对我有用的一个技巧是首先无条件设置标题:

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

And then to unset the header in case we are in devel mode: 然后在开发模式下取消设置标题:

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

This requires that you have a boolean env devel previously initialized based on your mode. 这要求您具有一个先前根据您的模式初始化的布尔型env devel In our case we decide on the host name whether we want to be devel or not (devel.domain.com vs. www.domain.com). 在我们的案例中,我们决定是否要使用主机名(devel.domain.com与www.domain.com)。

Starting from what alienhard said I managed to come up with an answer to my problem. 从alienhard所说的开始,我设法为我的问题提供了答案。

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+) is the folder of images for a particular project number (\\d+) ) /images/(\\d+)是特定项目编号(\\d+)的图像文件夹)

The E flag of rewriterule lets you set an environment variable in the case that the rule matches. rewriteruleE标志使您可以在规则匹配的情况下设置环境变量。 - doesn't actually rewrite anything. -实际上没有重写任何内容。 Thus, this checks the output of the script using rewritecond sending it the project number from the rewriterule , and then sets the environment variable in the case that both conditions match. 因此,该检查使用脚本的输出rewritecond从发送它的项目数rewriterule ,然后设置环境变量中,这两个条件匹配的情况。 Then header conditionally gets set based on the presence of that environment variable. 然后根据该环境变量的存在条件有条件地设置header

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM