10

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?

sysadmin1138
  • 133,124
  • 18
  • 176
  • 300
Jakob Borg
  • 1,453
  • 1
  • 10
  • 13

2 Answers2

12

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.

miishuu
  • 136
  • 3
0

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.

  • 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