2

I have this little .htaccess file:

RewriteEngine on
RewriteRule ^(.*)$ urlroute.php?q=$1 [E=myvariable:'withthisvalue',B,QSA]

To direct all requests to my URL routing script. It works great and I can access the requested URL in urlroute.php as $_GET['q'].

I'm setting the environment variable myvariable using mod_rewrite I am able to access it in urlroute.php as $_SERVER['myvariable']. I am not able to access it using $_ENV['myvariable'] which seems strange. Could someone please explain what the reason could be?

To my knowledge, mod_redirect will prepend REDIRECT_ to environment variable names when doing a redirect (which it always does in my case). But for me both $_SERVER['myvariable'] and $_SERVER['REDIRECT_myvariable'] are available and set to the same content. Why?

1 Answer 1

4

See this post also discusses the same feature. The rewrite engine loops on evaluating .htaccess files at the start of each cycle the engine copies any environment variables into a copy REDIRECT_*. Hence if the parsing of the .htaccess files requires 3 loops then you will also get REDIRECT_REDIRECT_* files, and so on.

Apache 2.3 includes a new [E] flag but you can used this feature to implement the same in earlier versions:

RewriteCond %{ENV:REDIRECT_END} =1
RewriteRule ^   -  [L]   

# other rules

...

RewriteRule somepattern  somesubst   [L,E=END:1]

AFAIK, these variables will available the SERVER context, but whether they are available in the ENVIRONMENT depends on how PHP is implemented e.g. Apache+mod_php, Apache+mod_suphp, Apache+mod_fcgi, IIS, ...

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

2 Comments

+1 for good answer, however I think you meant RewriteCond instead of RedirectCond.
@anubhava, that's what I call a "mindfart". Sorry. of course you are right. I've edited the answer

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.