Doing a redirect in Apache is easy (mod_alias):

RedirectMatch ^.*$ http://portal.example.com/

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 ^.*$ http://portal.example.com/

Example of what actually happens:

jb@apto % telnet 80
Connected to redirector.example.com.
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
Location: http://portal.example.com/
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?

2 Answers 2


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.

  • !!! Don't know how I missed this, but yes!
    – Jakob Borg
    Sep 28, 2010 at 6:29
  • Yes, this is it. Great.
    – Cosimo
    Jun 7, 2013 at 8:40
  • This doesn't seem to work for custom headers, strange
    – juan
    Jul 23, 2013 at 15:32

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 want...it's not pretty, but as far as I know it's the only way to do it.

  • 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, 2010 at 12:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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