Apache 2.4,嵌套的If指令 [英] apache 2.4, nested If directives

查看:103
本文介绍了Apache 2.4,嵌套的If指令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试通过以下指令块给apache配置文件中设置一些环境变量,方法是设置标头"X-Forwarded-Proto".

I try to set some environment variables in apache configuration file with the following directive block givet that the header "X-Forwarded-Proto" is set.

<If "-n req('X-Forwarded-Proto')">
  SetEnv NON_EMPTY_PROTO on
  <If "req('X-Forwarded-Proto') =~ /https/">
    SetEnv HTTPS on
  </If>
  <Else>
    SetEnv HTTPS off
  </Else>
</If>

结果,环境变量NON_EMPTY_PROTO设置为预期值,但未设置HTTPS变量. 但是,如果我避免只为测试而嵌套,则两个变量均按预期设置.

As a result the environment variable NON_EMPTY_PROTO is set as expected but the variable HTTPS is not. But if I avoid nesting just for test both variables are set as expected.

<If "-n req('X-Forwarded-Proto')">
  SetEnv NON_EMPTY_PROTO on
</If>

<If "req('X-Forwarded-Proto') =~ /https/">
  SetEnv HTTPS on
</If>
<Else>
  SetEnv HTTPS off
</Else>

为什么在第一个示例中未验证内部块?指令文档中未提及有关可能嵌套或不可能嵌套的任何内容.

Why is the inner block not validated in first example? The directives documentation does not mention anything about possible or not possible nesting.

顺便说一句,我知道我可以使用下面这样的逻辑等效项,但是如果情况变得更复杂,嵌套就更简单,更方便了.

By the way, I know I can use a logical equivalent like this below, but nesting is just simpler and more convinient if the scenario gets more complex.

<If "-n req('X-Forwarded-Proto') && req('X-Forwarded-Proto') =~ /https/">
  SetEnv HTTPS on
</If>
<ElseIf "-n req('X-Forwarded-Proto')">
  SetEnv HTTPS off
</ElseIf>

我明确询问嵌套版本在这里不起作用的原因.

I ask explicitely for the reason why the nested version does not work here.

推荐答案

更新后的答案:

从Apache 2.4.26开始,允许嵌套<If>指令.

As of Apache 2.4.26 nested <If> directives are allowed.

Apache 2.4 ChangeLog 进行了以下更改:

评估嵌套的If/ElseIf/Else配置块. [Luca Toscano,Jacob冠军]

Evaluate nested If/ElseIf/Else configuration blocks. [Luca Toscano, Jacob Champion]

问题中的嵌套<If>指令应在Apache 2.4.26及更高版本中有效.

The nested <If> directives in the question should work in Apache 2.4.26 and forward.

虽然我无法确认问题中的特定规则,但这是带有嵌套<If>指令的htaccess规则的有效示例:

While I could not confirm the specific rules in the question, here is a working example of htaccess rules with nested <If> directives:

RewriteEngine On

<If "%{REQUEST_URI} =~ /flintstones$/ ">
    <If "%{QUERY_STRING} == 'character=fred'">
        ErrorDocument 200 "Fred says, \"Yabba Dabba Doo\" <a href=\"flintstones\">back</a>"
    </If>
    <ElseIf "%{QUERY_STRING} == 'character=barney'">
        ErrorDocument 200 "Barney says, \"Uh hee hee hee... Okay Fred!\" <a href=\"flintstones\">back</a>"
    </ElseIf>
    <Else>
        ErrorDocument 200 "Pick a Flintstones character: <a href=\"flintstones?character=fred\">Fred</a> or <a href=\"flintstones?character=barney\">Barney</a>"
    </Else>
    RewriteRule ^ - [R=200,L]   
</If>


原始答案

<If>指令不能嵌套在另一个<If>指令内.

An <If> directive cannot be nested inside another <If> directive.

用于If指令的Apache 2.4文档具有以下通知:

不是脚本语言

该指令的名称是程序员和管理员非常熟悉的名称,但不应与脚本语言中的指令名称混淆.例如,当前的实现没有考虑在另一个内部包含<If>部分的可能性(内部的<If>将被忽略).

The name of this directive is very familiar to programmers and admins but it should not be confused with its counterpart in scripting languages. For example, the current implementation does not contemplate the possibility of having a <If> section inside another one (the inner <If> will be ignored).

没有给出为什么他们选择不允许嵌套的If指令的原因.

No reason is given for why they chose not to allow nested If directives.

文档中唯一的有关嵌套的其他信息表示每种节类型是否可以嵌套有所不同:

The only other information about nesting provided in the documentation indicates each section type differs on whether or not it can be nested:

节的嵌套

某些节类型可以嵌套在其他节类型中.一方面,可以在<Directory>内部使用<Files>.另一方面,<If>可以在<Directory><Location><Files>部分中使用(但不能在另一个<If>内部)使用.命名部分的正则表达式对应的行为相同.

Some section types can be nested inside other section types. On the one hand, <Files> can be used inside <Directory>. On the other hand, <If> can be used inside <Directory>, <Location>, and <Files> sections (but not inside another <If>). The regex counterparts of the named section behave identically.

这篇关于Apache 2.4,嵌套的If指令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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