I've got an Apache vhost which performs a number of rewrite operations (using mod_rewrite) on the requested URLs. Some of these RewriteRule calls redirect the browser to other hosts (using 301 and 302 redirections).

I have set a default expiry of 1 second in that vhost file:

ExpiresActive On
ExpiresDefault "access plus 1 second"

and accordingly, all of the redirections include this header:

Cache-Control: max-age=1

Now what I'm trying to do is to output a different caching header in the case of one particular redirection. I want most of the redirections (the 302s) to continue have a 1 second expiry, but for one of them (a 301), I'd like to use a 1-day expiry instead.

For this you need to make another virtual host that has the different settings, and for the redirects you want under "this situation" use that vHost instead. This is not PHP code, it's Apache language. Sorry. There are no if-thens.

One way of solving this problem without removing your default expiration set by mod_expires is to add a <Location> block for the URL that is being redirected. Let's say you're redirecting to a versioned folder and you will periodically change that version:

RewriteRule ^/approot/(.*) /approot.1.2.3.4/$1 [R=307,L]

If you don't want that redirect to be cached (but you do want actual content to be cached), just add this block to your configuration:

<Location /approot>
ExpiresActive Off
</Location>

Of course, "/approot" represents whatever URL you are redirecting.

Simple and easy. You don't have to fiddle with your already-working mod_expires configuration. You don't have to try to add or remove headers with mod_headers. You just tell mod_expires to not add its headers for the URL that you're redirecting.

I first tried the accepted answer above (by Francois Marier) but it didn't work because I didn't want to disable my ExpiresDefault setting and then have to replicate an expiration in many different areas of the configuration in its place.