简体   繁体   English

Apache'if'指令中的正则表达式...这里有什么问题?

[英]Regex in Apache 'if' directive … what's wrong here?

(A possible bug in Apache is now suspected ... see below.) (现在怀疑Apache中可能出现的错误......见下文。)

Here's the statements: (Apache 2.4) 这是陈述:( Apache 2.4)

<IfDefine !SERVER_TYPE>
    Define SERVER_TYPE prod
</IfDefine>
<If "${SERVER_TYPE} !~ /prod|demo|test/">   <===error here===
    Error "Define 'SERVER_TYPE' is hosed'"
</If>

Apache complains about the IF: Apache抱怨IF:

Cannot parse condition clause: syntax error, unexpected T_OP_NRE, expecting '('

I can guess that T_OP_NRE refers to "negated regular-expression" ie "!~" but I can't for the life of me figure out what's the syntax-error in that statement! 我可以猜测T_OP_NRE指的是“否定的正则表达式”, “!〜”但我不能为我的生活找出该语句中的语法错误! (And, incidentally, "=~" produces [almost] the same message.) (顺便说一下,“=〜”会产生[几乎]相同的信息。)

Apache is recognizing the ${SERVER_TYPE} syntax as I can confirm if I misspell the identifier. Apache正在识别$ {SERVER_TYPE}语法,因为我可以确认是否拼错了标识符。 The documentation for <DEFINE> expressly states that "${ident}" not "%{ident}" syntax should be used, as I have done. <DEFINE>的文档明确指出应该使用“$ {ident}”而不是“%{ident}”语法,正如我所做的那样。

What's wrong with my syntax, and why does Apache expect a left-parenthesis? 我的语法有什么问题,为什么Apache期望左括号?


I am cautiously beginning to believe that this is actually a BUG(!) in Apache/2.4.12 because the parse-error only occurs when "${DEFINED_SYMBOL}" is used. 我小心翼翼地开始相信这实际上是Apache / 2.4.12中的BUG(!) ,因为解析错误仅在使用“$ {DEFINED_SYMBOL}”时发生。 It will complain and do all the right things if the symbol is not defined (so, it knows what it's looking at ...), but it gives a nonsensical (to me ...) message as-shown if, and only if, this construct is on the left-hand side. 如果没有定义符号,它会抱怨并做所有正确的事情(因此,它知道它正在看什么...),但它给出了一个荒谬的(对我来说)消息如果显示的话,只有当,这个结构位于左侧。

An apparent workaround is to put the symbol in single quotes, eg: 一个明显的解决方法是将符号放在单引号中,例如:

<If "'${SERVER_TYPE}' != /prod/demo/test/">

... but so-far in my testing I don't think that it is matching the regex correctly. ...但是在我的测试中,我认为它并没有正确匹配正则表达式。 In fact, I'm not yet sure what is going on, because the following is considered to be a non -match ("true"): 事实上,我还不确定发生了什么 ,因为以下被认为是非匹配(“真实”):

<If "'prod' !~ /prod|demo|test/">

The regex syntax can be shown to be correct via this Perl one-liner: 通过这个Perl单行显示正则表达式语法是正确的:

perl -e 'my $var = "prod"; if ($var =~ /prod|demo|test/) {print "yes\n";}'

... which prints "yes." ...打印“是”。


PS The "${IDENT}" syntax is correct (versus "%{}", which is not) in the case of a Defined symbol. PS“$ {IDENT}”语法正确的(对于“%{}”,对于已定义的符号,则不是)。

Try this syntax: 试试这个语法:

<If "'${SERVER_TYPE}' !~ m#(prod|demo|test)#">
    #Error "Define 'SERVER_TYPE' is hosed"
    RedirectMatch /old /new/
</If>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM