Samul
Samul

Reputation: 2019

Header set with query_string

I am runing apache and I am trying to set a header Foo=bar only when the request has a variable "q" on the query string. I would like something like this in my htaccess:

<RequestUri "q=">
    Header set Foor "bar"
</RequestUri>

Of course it does not work. I already tried using "Location" and "LocationMatch" but those are not allowed inside the htaccess. So how can I do that?

Upvotes: 3

Views: 2253

Answers (2)

Adambean
Adambean

Reputation: 1161

Just to expand on this further if you want to set the header's value dynamically according to the value of the query string you can capture regex groups.

For example if you have a "_locale" variable in your URL and you want to capture its value for a header you could use:

 <If %{QUERY_STRING} !~ m#_locale=([a-z]{2})#">
     RequestHeader set locale "$1"
</If>

(Assuming the value will be two lower case letters.)

This could match "_locale=en" in the query string then copy "en" to header "locale".

Upvotes: 1

Panama Jack
Panama Jack

Reputation: 24468

If you are using Apache 2.4, you can do something like this

<If "%{QUERY_STRING} =~ /q=.*?/">
    Header set Foo "bar"
</If>

https://httpd.apache.org/docs/2.4/mod/core.html#if

https://httpd.apache.org/docs/2.4/expr.html#examples

Upvotes: 5

Related Questions