Need to add request headers to every request in Apache

apache-2.2requestheader

I'm trying to add a header value to every request via Apache (ver 2.2).
I've edited my VirtualHost to include the following vaiations:
(I've tried both RequestHeader and Header, add and set in all of these cases)

RequestHeader set X-test_url "Test"

or

<Directory />
  RequestHeader set X-test_url "Test"
</Directory>

or

<Location ~ "/*" >
  RequestHeader set X-test_url "Test"
</Location>

It's hard to explain how I've gotten to this point, but I have to get this done in Apache. Again I'm trying to add the header value to every request.
Thanks.

Best Answer

So your first ought work alright. I've just tested the following. On a Red Hat system, I added RequestHeader add X-LocalHeader "Headers For the Win" to my httpd.conf file.

Then, I put together a quick Python script to dump my environment:


#!/usr/bin/python

import os
print 'Content-type: text/plain'
print

for tup in os.environ.items():
    print '%s: %s' % tup

Finally, an Apache restart and a curl yields the following:

[jeff@marvin ~]$ curl http://localhost/cgi-bin/test.py
HTTP_ACCEPT: */*
HTTP_USER_AGENT: curl/7.19.7 
SERVER_NAME: localhost
REMOTE_ADDR: 127.0.0.1
SERVER_PROTOCOL: HTTP/1.1
SCRIPT_FILENAME: /var/www/cgi-bin/test.py
REMOTE_PORT: 42551
SERVER_SOFTWARE: Apache/2.2.13 (Fedora)
SERVER_ADMIN: root@localhost
SCRIPT_NAME: /cgi-bin/test.py
SERVER_SIGNATURE: Apache/2.2.13 

REQUEST_METHOD: GET
HTTP_HOST: localhost
SERVER_PORT: 80
GATEWAY_INTERFACE: CGI/1.1
QUERY_STRING: 
PATH: /sbin:/usr/sbin:/bin:/usr/bin
REQUEST_URI: /cgi-bin/test.py
HTTP_X_LOCALHEADER: Headers For the Win
SERVER_ADDR: 127.0.0.1
DOCUMENT_ROOT: /var/www/html

As you can see, I have an 'HTTP_X_LOCALHEADER' value set, which corresponds to the header we added earlier. It looks like you had it right, is it still not working?