访问在PHP中使用mod_rewrite设置的环境变量 [英] Accessing environment variables set using mod_rewrite in PHP

查看:64
本文介绍了访问在PHP中使用mod_rewrite设置的环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个.htaccess小文件:

I have this little .htaccess file:

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

将所有请求定向到我的URL路由脚本.效果很好,我可以在urlroute.php中以$_GET['q']的形式访问请求的URL.

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'].

我正在使用mod_rewrite设置环境变量myvariable 我可以作为$_SERVER['myvariable']在urlroute.php中访问它.我无法使用$_ENV['myvariable']访问它,这似乎很奇怪.有人可以解释原因吗?

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?

据我所知,在进行重定向时,mod_redirect会将REDIRECT_放在环境变量名称的前面(在我的情况下,通常如此).但对我来说,$_SERVER['myvariable']$_SERVER['REDIRECT_myvariable']均可用,并设置为相同的内容.为什么?

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?

推荐答案

请参见这篇文章也讨论了相同的功能.在每个循环开始时,重写引擎都会在评估.htaccess文件时循环执行,该引擎会将所有环境变量复制到副本 REDIRECT _ * 中.因此,如果.htaccess文件的解析需要3个循环,那么您还将获得 REDIRECT_REDIRECT _ * 文件,依此类推.

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包含一个新的[E]标志,但是您可以使用此功能在较早的版本中实现相同的功能:

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,这些变量将在 SERVER 上下文中可用,但是在环境中它们是否可用取决于如何实现PHP,例如Apache + mod_php,Apache + mod_suphp,Apache + mod_fcgi,IIS,...

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, ...

这篇关于访问在PHP中使用mod_rewrite设置的环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆