问题 htaccess可以读取Apache中设置的服务器环境变量吗?


在Apache设置中设置服务器环境变量时,可以使用内置函数从PHP读取该变量。

但是你能从htaccess文件中读取变量吗?

我想要在我的htaccess中完成的是:

<If var=="stuff">
do things here
</if>

<if var=="different stuff">
do different things here
</if>

11364
2017-09-04 19:01


起源



答案:


是的。

您可能希望使用mod_setenvif功能,因此需要打开该模块。

然后你只需要做一些事情:

SetEnvIf Request_URI "\.gif$" image_type=gif

哪个会设置环境变量 image_type 价值为 gif 如果请求的文件名以。结尾 .gif

然后你可以像这样访问它(比如在RewriteCond中):

RewriteCond %{ENV:image_type} gif
RewriteRule ^(.*) $.gif

您可以在此处阅读完整文档: http://httpd.apache.org/docs/2.2/env.html


6
2017-09-04 19:11



%{ENV:variable}部分只是检查“变量”是否存在?有没有办法检查变量的值是什么? - Tim Jahn
@TimJahn NO实际上给出了变量的值。我编辑过可能会回答希望给出更好的用法示例。虽然在我的情况下,这个例子很无用。 - Mike Brant
嗯,似乎不适合我。这是我的上下文代码: RewriteEngine on RewriteCond %{ENV:ENVIRONMENT} production RewriteCond %{REQUEST_URI} !/index.php RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ /index.php/$1 [L] - Tim Jahn
@TimJahn你在哪里设置你的环境变量? - Mike Brant
我在httpd.conf中设置环境变量 - Tim Jahn


答案:


是的。

您可能希望使用mod_setenvif功能,因此需要打开该模块。

然后你只需要做一些事情:

SetEnvIf Request_URI "\.gif$" image_type=gif

哪个会设置环境变量 image_type 价值为 gif 如果请求的文件名以。结尾 .gif

然后你可以像这样访问它(比如在RewriteCond中):

RewriteCond %{ENV:image_type} gif
RewriteRule ^(.*) $.gif

您可以在此处阅读完整文档: http://httpd.apache.org/docs/2.2/env.html


6
2017-09-04 19:11



%{ENV:variable}部分只是检查“变量”是否存在?有没有办法检查变量的值是什么? - Tim Jahn
@TimJahn NO实际上给出了变量的值。我编辑过可能会回答希望给出更好的用法示例。虽然在我的情况下,这个例子很无用。 - Mike Brant
嗯,似乎不适合我。这是我的上下文代码: RewriteEngine on RewriteCond %{ENV:ENVIRONMENT} production RewriteCond %{REQUEST_URI} !/index.php RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ /index.php/$1 [L] - Tim Jahn
@TimJahn你在哪里设置你的环境变量? - Mike Brant
我在httpd.conf中设置环境变量 - Tim Jahn


Apache 2.4推出 <If><Else><ElseIf> 阻止与新的表达式语法结合使用,以便在.htaccess文件等中实现更强大的编程控制。

这是未经测试但表面上有效:

<If "%{MY_VAR} == 'my result'">
    …
</If>

资源: https://blogs.apache.org/httpd/entry/new_in_httpd_2_4


3
2017-12-02 00:57





因为我在设置类似的事情时遇到了很多问题,认识到我的家庭本地主机与我的工作设置截然不同,所以这就是我最终要做的事情。

首先,The <IF ..> 指令不识别SetEnv。从 http://httpd.apache.org/docs/current/env.html :“SetEnv指令在请求处理期间运行较晚,这意味着诸如SetEnvIf和RewriteCond之类的指令不会看到用它设置的变量。”

所以,如果可以,尝试使用SetEnvIf。我很懒,把它放在我的httpd.conf文件中。它检查它是否在localhost上运行 - 显然是 - 然后设置变量:

SetEnvIf Server_Addr ^127\.0\.0\.1$ home_variable

然后,在其他地方,在我的htaccess文件中,我有这个:

# IfVersion needs mod_version - you could do an ifmodule before all this i guess
# we use ifversion because If was introduced in apache 2.4
<IfVersion >= 2.4>
  # check if the home variable exists/isn't: empty, "0", "off", "false", or "no"
  <If "-T reqenv('home_variable')">
    ... do home-y stuff
  </If>

  <Else>
    ... do work-y stuff
  </Else>
</IfVersion>

# fallback
<IfVersion < 2.4>
  .. fallback?
</IfVersion>

或者,您可以为每个环境设置一个变量,并为其指定一个特定值:

SetEnvIf Server_Addr ^127\.0\.0\.1$ check_location=home

其余的将是相同的,除了第一个<If ..> 声明:

<If "%{ENV:check_location} == 'home'">....</If>

我已经在2.4的家庭环境和2.2的工作环境之间测试了这两者,两者都启用了mod_version(因此没有烦扰ifmod)。

(我还没有测试过两个以上的环境,但Apache 2.4确实提供了一个 <ElseIf> 所以这也是一个选择)。

Doop di do。

(关于-T运算符等的更多信息: http://httpd.apache.org/docs/current/expr.html


3
2017-08-02 11:17



应该提到的是 <If> 从Apache 2.4开始提供。 - David


我在这里给出了描述和.htaccess以及PHP示例: 在.htaccess中设置一个环境变量并在PHP中检索它

对于.htaccess中的一些示例,取自我修改Perishable Press 5G黑名单/防火墙 http://perishablepress.com/5g-blacklist-2013/ 使用环境变量报告:

SetEnv myServerName %{SERVER_NAME}

RewriteCond %{QUERY_STRING} (base64_encode|localhost|mosconfig|open_basedir) [NC,OR]
RewriteCond %{QUERY_STRING} (boot\.ini|echo.*kae|etc/passwd) [NC,OR]
RewriteCond %{QUERY_STRING} (GLOBALS|REQUEST)(=|\[|%) [NC]
RewriteRule .* - [E=badQueryString:%0--%1--%2,F,L]

SetEnvIfNoCase User-Agent ^$ noUserAgent
SetEnvIfNoCase User-Agent (binlar|casper|cmsworldmap|comodo|diavol|dotbot|feedfinder|flicky|ia_archiver|jakarta|kmccrew|nutch|planetwork|purebot|pycurl|skygrid|sucker|turnit|vikspider|zmeu) badUserAgent=$1

<limit GET POST PUT>
  Order Allow,Deny
  Allow from all
  Deny from env=badUserAgent
</limit>

注意使用参数。 %0给出完整字符串,%1给出第一个带括号的语句匹配,%2给出第二个。连字符是文字​​显示字符,用于在视觉上分开(不要认为有任何方法可以在其中放置空格)。


1
2017-09-18 00:51