Setting http headers with Apache and mod_headers

Continuing my HTTP bender, I'd like to discuss some fun / necessary things you can do to manipulate HTTP headers using the Apache mod_headers module.

If you were to make a request to Slashdot and examine the Http headers using either HttpLiveHeaders or FireBug you would notice one of two unusual headers: X-Bender, and X-Fry with quirky messages. These are pop-culture references to the show "Futurama".

There are a few ways you could accomplish this -- if you were already working with mod_perl, mod_python, or PHP it would be trivial to manipulate the headers programatically. However, it's also possible to do so with a simple one liner in your Apache conf.

First you'll need to enable mod_headers -- On my Debian server this was done with the following command: a2enmod headers, after which I had to restart Apache.

Then simply add something like the following line in your conf, of course replacing "HeaderNameHere" and "Header Value" with whatever you want.

Header add HeaderNameHere "Header Value here"

The "Header" directive has a number of possible actions including: set, unset, append, add and echo. The documentation is fairly well written and can be found here: mod_headers documentation

One useful application would be in a load-balanced apache setup, you could set the actual hostname as a header for debugging purposes. You can echo environment variables like so: %{FOOBAR}e which would display the "FOOBAR" variable.

Have fun tweaking Apache headers, hope this helps.