To set RequestHeader in apache httpd server is not working

apache-2.2httpd.confmod-proxymod-rewriterewritecond

I have a requirement, where I need to set the RequestHeader with a value retrieved from the querystring of the URI.

Approach adopted is to write the RewriteCond and ReWriteRule where
the condition is to extract value from %{QUERYSTRING} variable and set this into another Environment variable and later refer this and add it to request header.

But this doesn't seem to be working for me. I am using Apache 2.2 on RHEL.

My httpd configuration placed in one of .conf file is as below.

RewriteCond %{QUERY_STRING} ^(\w+)=(\w+)$
RewriteRule . - [E=RU:%1]
Header set "X-Header" "%{RU}e" env=RU

NOTE: I also tried to set the request header by hard coding the value and I also have proxy module enabled.

RequestHeader set X-User-ID "test"
Header append X-test %{RU}e
RequestHeader set X-UserID %{RU}e early

Using "early" option also didn't help.

Best Answer

In your original configuration you are using Header instead of RequestHeader. Header sets a response header not a request header.

I've tested it by adding the logging of the X-header HTTP header to the access log format, and it works fine. The only thing I've changed is the . to a ^ in the RewriteRule

RewriteEngine On
RewriteCond %{QUERY_STRING} ^(\w+)=(\w+)$
RewriteRule ^ - [E=RU:%1]
RequestHeader set "X-Header" "%{RU}e" env=RU

How are you testing the header has been set?