【问题标题】:Can htaccess read a server environment variable set in Apache?htaccess 可以读取 Apache 中设置的服务器环境变量吗?
【发布时间】:2012-08-29 11:35:58
【问题描述】:

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

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

我试图在我的 htaccess 中完成的事情是:

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

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

【问题讨论】:

    标签: apache .htaccess webserver


    【解决方案1】:

    是的。

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

    然后您只需执行以下操作:

    SetEnvIf Request_URI "\.gif$" image_type=gif
    

    如果请求的文件名以.gif 结尾,这会将环境变量image_type 设置为gif

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

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

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

    【讨论】:

    • %{ENV:variable} 部分是否只是检查“变量”是否存在?有没有办法检查变量的值是什么?
    • @TimJahn NO 实际上给出了变量的值。我已经编辑过可能会回答希望给出更好的使用示例。虽然在我的例子中这个例子没什么用。
    • 嗯,似乎对我不起作用。这是我的上下文代码:RewriteEngine on RewriteCond %{ENV:ENVIRONMENT} production RewriteCond %{REQUEST_URI} !/index.php RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ /index.php/$1 [L]
    • @TimJahn 您实际上在哪里设置环境变量?
    • 啊...问题是常规环境变量在请求过程中设置得很晚,并且对.htaccess 不可用。您需要在 .htaccess 中使用 SetEnvIf 设置值。请参阅此文档链接中的“警告”部分。 httpd.apache.org/docs/2.2/env.html
    【解决方案2】:

    由于我在设置类似的东西以识别我的家庭本地主机与我的工作设置不同时遇到了很多问题,这就是我最终要做的事情。

    首先,&lt;IF ..&gt; 指令无法识别 SetEnv。来自http://httpd.apache.org/docs/current/env.html:“SetEnv 指令在请求处理期间运行较晚,这意味着 SetEnvIf 和 RewriteCond 等指令将看不到使用它设置的变量。”

    因此,如果可以,请尝试使用 SetEnvIf。我很懒,只是把它放在我的 httpd.conf 文件中。它检查它是否在本地主机上运行 - 这显然是 - 然后设置变量:

    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
    

    那么其余的都是一样的,除了 first&lt;If ..&gt; 声明:

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

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

    (我也没有测试两个以上的环境,但 Apache 2.4 确实提供了&lt;ElseIf&gt;,所以这也是一个选项)。

    Doop di do。

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

    【讨论】:

    • 应该提到&lt;If&gt; 从 Apache 2.4 开始可用。
    【解决方案3】:

    Apache 2.4 引入了 &lt;If&gt;&lt;Else&gt;&lt;ElseIf&gt; 块以及新的表达式语法,以便在 .htaccess 文件等中实现更强大的编程控制。

    这未经测试,但表面上可行:

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

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

    【讨论】:

      【解决方案4】:

      我在这里给出了描述和 .htaccess 和 PHP 示例:Set an environment variable in .htaccess and retrieve it in 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 给出第二个。连字符是文字​​显示字符,在视觉上是分开的(不要认为有任何方法可以在其中放置空格)。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-06-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-04-07
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多