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.

Colt
  • 2,029
  • 6
  • 21
  • 27
Usman Azhar
  • 111
  • 1
  • 1
  • 2
  • 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. – Cameron Kerr Jun 24 '16 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, – Cameron Kerr Jun 24 '16 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. – Usman Azhar Jun 27 '16 at 09: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". – Usman Azhar Jul 05 '16 at 06:59

2 Answers2

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?

Unbeliever
  • 2,336
  • 1
  • 10
  • 19
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.