1

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.

4
  • Without looking further, it looks like you likely need RewriteEngine On just before the RewriteCond. I struck an issue today where I needed to specify RewriteEngine On again after another existing RewriteEngine On; RewriteCond ...; RewriteRule ... configuration. Commented Jun 24, 2016 at 11:43
  • Also, this strategy is likely to be fragile. You could have this path instead be sent to a script that more robustly parses the query-string arguments and then redirects accordingly, Commented Jun 24, 2016 at 11:46
  • @Cameron, Thanks for you input , But adding RewriteEngine On just before the RewriteCond didn't help. And by enabling Rewrite Log , in the logs able to see that value is set to Environment variable. But RequestHeader and Header is not able to read this value. Commented Jun 27, 2016 at 9:45
  • I am able to add the request header but the scope of this Environment used is valid only for the first request. and EU is not visible, since i am doing redirect using javascript. Also i was looking at the redirecred page in firefox browser, where it shows as "null". Commented Jul 5, 2016 at 6:59

2 Answers 2

1

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?

0

Judging by your last comment, it looks like you are trying to set an environment variable that survives external redirects. This can't be done with environment variables or request header as they are scoped per request. However, you can do it by setting cookies in your RewriteRules using the CO flag.

The solution suggested by @Unbeliever works best when you want your environment variables to survive internal redirects performed by Apache mod_rewrite. Keep in mind that the custom header may not be immediately available for evaluation.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .