0

I'm trying to set an Environment variable if I got a specific HTTP Header sent.

So I tried a few different way that I will detail

Header set MY_HTTP_HEADER "1"

# tests with with SetEnvIf
SetEnvIf %{HTTP:MY_HTTP_HEADER} ^1$ THE_ENV=ok
SetEnvIf MY_HTTP_HEADER ^1$ THE_ENV=ok
SetEnvIf %{MY_HTTP_HEADER} ^1$ THE_ENV=ok

# tests with RewriteRule
RewriteCond %{HTTP:MY_HTTP_HEADER} ^1$
RewriteRule .* index.php [L,E=THE_ENV:ok]

RewriteCond MY_HTTP_HEADER ^1$
RewriteRule .* index.php [L,E=THE_ENV:ok]

There is something that I certainly missed, because all the codes above doesn't work.

EDIT
The correct one is SetEnvIf MY_HTTP_HEADER ^1$ THE_ENV=ok and like @anubhava pointed it doesn't work if you set the header in the same .htaccess so I created another page calling the actual page with CURL with this header curl_setopt($curl,CURLOPT_HTTPHEADER,array('MY_HTTP_HEADER: 1'));

5
  • So which variable is not being set from above code? Commented Nov 5, 2013 at 15:45
  • I put all of them but I was testing it 1 by 1 of course and deactivating the others. Commented Nov 5, 2013 at 15:47
  • How are you sending MY_HTTP_HEADER header in the request? Commented Nov 5, 2013 at 16:39
  • I don't really understand what you mean by that... I set it in the .htaccess on the first line, am I doing it wrong? Commented Nov 5, 2013 at 17:10
  • Yes, see my answer for details. Commented Nov 5, 2013 at 17:22

1 Answer 1

2

You're making 2 mistakes:

1) This directive:

Header set MY_HTTP_HEADER "1"

Actually sends response header not request header. Use it like this to set request header:

RequestHeader set X-MY-HTTP-HEADER "1"

2) You're setting request header and checking for it in the same .htaccess. Try sending header in web request from browser (using some Rest client addon) and then you will find THE_ENV=ok env value in your index.php

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for your reply, just a question is Header and Env the same thing? Because in PHP if I print all SERVER information and I do SetEnv TEST_OK 123 and RequestHeader set TEST_OK "456" they both appear but the first with TEST_OK and the other one HTTP_TEST_OK... Ok I will try to understand a bit more thanks.
env is in the context of a server request. headers are part of HTTP request and response that travel between client and server.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.