Frage

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?

War es hilfreich?

Lösung

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. In our case we decide on the host name whether we want to be devel or not (devel.domain.com vs. www.domain.com).

Andere Tipps

Starting from what alienhard said I managed to come up with an answer to my problem.

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+))

The E flag of rewriterule 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 using rewritecond sending it the project number from the rewriterule, and then sets the environment variable in the case that both conditions match. Then header conditionally gets set based on the presence of that environment variable.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top