Apache'if'指令中的正则表达式...这里出了什么问题? [英] Regex in Apache 'if' directive ... what's wrong here?
问题描述
(现在怀疑是Apache中的一个可能的错误...请参阅下文.)
以下是这些语句:(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抱怨IF:
Apache complains about the IF:
Cannot parse condition clause: syntax error, unexpected T_OP_NRE, expecting '('
我可以猜到T_OP_NRE是指否定的正则表达式".即!〜",但是我一生都无法弄清楚该语句中的语法错误是什么! (而且,偶然地,"=〜"会产生[几乎]相同的消息.)
I can guess that T_OP_NRE refers to "negated regular-expression" i.e. "!~" but I can't for the life of me figure out what's the syntax-error in that statement! (And, incidentally, "=~" produces [almost] the same message.)
Apache可以识别$ {SERVER_TYPE}语法,因为我可以确认是否拼写错误的标识符. < DEFINE>的文档明确指出,应该像我一样使用"$ {ident}"而不是%{ident}"语法.
Apache is recognizing the ${SERVER_TYPE} syntax as I can confirm if I misspell the identifier. The documentation for <DEFINE> expressly states that "${ident}" not "%{ident}" syntax should be used, as I have done.
我的语法有什么问题,为什么Apache期望使用左括号?
What's wrong with my syntax, and why does Apache expect a left-parenthesis?
我开始谨慎地认为这实际上是Apache/2.4.12中的 BUG(!),因为仅当使用"$ {DEFINED_SYMBOL}"时才会发生解析错误.如果未定义该符号,它将抱怨并做所有正确的事情(因此,它知道它在看什么...),但是它给出了一个荒谬的(对我...)消息,如下所示: -仅当此结构位于左侧时显示.
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. 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, e.g.:
<If "'${SERVER_TYPE}' != /prod/demo/test/">
...但是到目前为止,在我的测试中,我认为它与正则表达式不正确匹配.实际上,我不确定发生什么,因为以下内容被认为是非匹配("true"):
... 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/">
通过以下Perl单行代码,可以证明正则表达式的语法是正确的:
The regex syntax can be shown to be correct via this Perl one-liner:
perl -e 'my $var = "prod"; if ($var =~ /prod|demo|test/) {print "yes\n";}'
...显示是".
P.S.对于已定义符号,"$ {IDENT}"语法 是正确的(与%{}"相反).
P.S. The "${IDENT}" syntax is correct (versus "%{}", which is not) in the case of a Defined symbol.
推荐答案
尝试以下语法:
<If "'${SERVER_TYPE}' !~ m#(prod|demo|test)#">
#Error "Define 'SERVER_TYPE' is hosed"
RedirectMatch /old /new/
</If>
这篇关于Apache'if'指令中的正则表达式...这里出了什么问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!