Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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/
</VirtualHost>

Example of what actually happens:

jb@apto % telnet 192.168.0.1 80
Trying 192.168.0.1...
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

(etc)

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

share|improve this question

2 Answers 2

up vote 4 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 –  jmfsg Jul 23 '13 at 15:32
    
If you want "always" headers only for selected redirects, see this: mark.koli.ch/… –  Mikko Rantalainen Sep 15 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":

http://httpd.apache.org/docs/2.2/misc/rewriteguide.html

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.

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

 
discard

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.