Search code examples
apachemod-rewritehttp-headersapache-configmod-expires

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. 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?


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