在 Apache 配置中有条件地设置过期标头
我想有条件地在图像上设置过期标头,以便它们在项目开发过程中不会缓存,但在生产过程中会缓存。理想情况下,这只是对 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"
虽然这当然行不通。
有什么解决办法吗?
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. I have a perl script that will return the status of the project, which can be used with mod_rewrite as follows:
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对我有用的一个技巧是首先无条件设置标头:
然后在我们处于开发模式时取消设置标头:
这要求您有一个预先根据您的模式初始化的布尔环境
devel
。在我们的例子中,我们决定是否要开发(devel.domain.com 与 www.domain.com)的主机名。A trick that worked for me is to first set the headers unconditionally:
And then to unset the header in case we are in devel mode:
This requires that you have a boolean env
devel
previously initialized based on your mode. In our case we decide on the host name whether we want to be devel or not (devel.domain.com vs. www.domain.com).从alienhard所说的开始,我设法找到了我的问题的答案。
(
/images/(\d+)
是特定项目编号(\d+)
的图像文件夹)E
标志>rewriterule 允许您在规则匹配的情况下设置环境变量。-
实际上并没有重写任何东西。因此,这会使用rewritecond
向其发送来自rewriterule
的项目编号来检查脚本的输出,然后在两个条件都匹配的情况下设置环境变量。然后根据该环境变量的存在有条件地设置header
。Starting from what alienhard said I managed to come up with an answer to my problem.
(
/images/(\d+)
is the folder of images for a particular project number(\d+)
)The
E
flag ofrewriterule
lets you set an environment variable in the case that the rule matches.-
doesn't actually rewrite anything. Thus, this checks the output of the script usingrewritecond
sending it the project number from therewriterule
, and then sets the environment variable in the case that both conditions match. Thenheader
conditionally gets set based on the presence of that environment variable.