问题
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:
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, ...
来源:https://stackoverflow.com/questions/9154695/accessing-environment-variables-set-using-mod-rewrite-in-php