Accessing environment variables set using mod_rewrite in PHP

只谈情不闲聊 提交于 2019-12-13 15:36:41

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!