1

This is just plain weird.

I have put the following in an .htaccess file:

RewriteRule ^a-file-on-the-server$ index.php [E=let_me_in:test]

And in my PHP script, I have the following:

print_r($_ENV);

...which prints out all the environment variables.

When I go to mydomain.com/a-file-on-the-server, I get the output:

Array
(
    [DOCUMENT_ROOT] => ********
    [GATEWAY_INTERFACE] => CGI/1.1
    [HTTP_ACCEPT] => application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
    [HTTP_ACCEPT_CHARSET] => ISO-8859-1,utf-8;q=0.7,*;q=0.3
    [HTTP_ACCEPT_ENCODING] => gzip,deflate,sdch
    [HTTP_ACCEPT_LANGUAGE] => en-US,en;q=0.8
    [HTTP_CACHE_CONTROL] => max-age=0
    [HTTP_CONNECTION] => keep-alive
    [HTTP_COOKIE] => ********
    [HTTP_HOST] => ********
    [HTTP_USER_AGENT] => Mozilla/5.0 (X11; U; Linux x86_64; en-US) AppleWebKit/533.4 (KHTML, like Gecko) Chrome/5.0.375.99 Safari/533.4
    [PATH] => /bin:/usr/bin
    [QUERY_STRING] => 
    [REDIRECT_STATUS] => 200
    [REMOTE_ADDR] => ********
    [REMOTE_PORT] => 36345
    [REQUEST_METHOD] => GET
    [REQUEST_URI] => ********
    [SCRIPT_FILENAME] => ********
    [SCRIPT_NAME] => ********
    [SERVER_ADDR] => ********
    [SERVER_ADMIN] => ********
    [SERVER_NAME] => ********
    [SERVER_PORT] => 80
    [SERVER_PROTOCOL] => HTTP/1.1
    [SERVER_SIGNATURE] => 
    [SERVER_SOFTWARE] => Apache
    [UNIQUE_ID] => ********
)

As you can see, the environment variable is not showing up. What am I doing wrong?

3 Answers 3

1

The one thing that looks wrong is the regex you're using. Remember that the regex matches the whole request URI, which always starts with a /. The way you've written it, it'll only match if the request starts with a a-file... which will never be the case.

Try using this instead (I've only added a / after the ^):

RewriteRule ^/a-file-on-the-server$ index.php [E=let_me_in:test]

There might be other parts of your rule that aren't right, but I don't have a lot of experience with environment vars.

0

hey I am doing some test and when setting env var,

I do what you do like [E=variable:value,other flag/value,(..)]

and in my php file I see those vat settings in $_SERVER['variable']

you might want to get your var like that:

echo $_SERVER['let_me_in'];

shoult output test.

1
  • I'm afraid that I don't understand your answer. Sep 22, 2010 at 3:05
0

I ran across this issue today doing some Apache mod_rewrite work. The issue is that PHP puts the environment variable defined as part of the rewrite rule into $_SERVER, not $_ENV (and it mutates the env var name).

Including a RewriteRule such as

RewriteRule ^a-file-on-the-server$ index.php [E=let_me_in:test]

in my setup (CentOS6 with Apache 2.2.15 and PHP 5.3.3), will provide the variable as $_SERVER['REDIRECT_LET_ME_IN'], such that index.php with contents:

<?php
   print_r($_SERVER['REDIRECT_LET_ME_IN']);
?>

would display the text:

test

I used the phpinfo() function call to investigate this issue and recommend it for viewing all available $_ENV and $_SERVER key/value pairs while debugging.

You must log in to answer this question.

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