Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Doing a redirect in Apache is easy (mod_alias):

RedirectMatch ^.*$

Setting cache headers is equally easy:

Header set Cache-Control max-age=0
Header set Expires "Thu, 01 Dec 1994 16:00:00 GMT"

(I don't want this cached)

But! It seems you can't combine the two. This configuration results in the redirect being sent, but not the the headers:

<VirtualHost *:80>
        ServerName __default__
        Header set Cache-Control max-age=0
        Header set Expires "Thu, 01 Dec 1994 16:00:00 GMT"
        RedirectMatch ^.*$

Example of what actually happens:

jb@apto % telnet 80
Connected to
Escape character is '^]'.
GET / HTTP/1.1
Host: foo

HTTP/1.1 302 Found
Date: Sat, 21 Aug 2010 09:36:38 GMT
Server: Apache/2.2.9 (Debian) Phusion_Passenger/2.2.9
Vary: Accept-Encoding
Content-Length: 316
Content-Type: text/html; charset=iso-8859-1


Any ideas for how to return a redirect with cache headers?

share|improve this question
up vote 6 down vote accepted

Try adding the "always" condition to your Header directive, so it should look like this:

Header always set Cache-Control max-age=0
Header always set Expires "Thu, 01 Dec 1994 16:00:00 GMT"

This should work, without the "always" condition I believe it defaults to "onsuccess" which is defined as any 2xx response code.

share|improve this answer
!!! Don't know how I missed this, but yes! – Jakob Borg Sep 28 '10 at 6:29
Yes, this is it. Great. – cosimo Jun 7 '13 at 8:40
This doesn't seem to work for custom headers, strange – juan Jul 23 '13 at 15:32
If you want "always" headers only for selected redirects, see this:… – Mikko Rantalainen Sep 15 '14 at 7:00

You'll need to implement a middle-man script in Perl or PHP (I'd use PHP, it's simpler if already loaded). Check out the rewrite guide, search for "Extended Redirection":

Set up the xredirect, then set your script to push out the headers you's not pretty, but as far as I know it's the only way to do it.

share|improve this answer
Not pretty indeed, I'm pretty surprised this isn't doable in a better way (directly in the configuration), but this looks like a correct answer. :( – Jakob Borg Aug 22 '10 at 12:57

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.