Apache-2.2
奇怪的 apache 重寫正則表達式
我正在將設置從 apache 遷移到 nginx。在這個過程中,我在 .htaccess 文件中遇到了這個重寫規則:
RewriteRule ^/?(?:(?!one|two|three|four).?)+/?$ http://somewhere.else.com [R=301,L]
我通常很擅長正則表達式,但這超出了我的範圍。首先,我什至不知道嵌入括號是允許的。有人可以向我解釋這個正則表達式嗎?如果它只是 apache,我如何在 nginx 中複製它?
(?!one|two|three|four)
意思是“不是(一或二或三或四)”。- 表示非擷取組(因此
?:
您不能使用$N
, 例如引用它$1
)。- 總之,它幾乎意味著任何沒有“一”或“二”或“三”或“四”序列的文本。
例如:
此 URL
/categories/category-1/hello-kitten/
如果應用於上述規則,將被重定向。但是這個/categoneries/category-1/hello-kitten/
不會,因為它有序列一:/categoneries/category-1/hello-kitten/
如果有幫助,這裡有一些更具體和詳細的資訊:
' ^/?(?:(?!one|two|three|four).?)+/?$ http://somewhere.else.com [R=301,L] ' ' Options: case insensitive ' ' Assert position at the beginning of the string «^» ' Match the character “/” literally «/?» ' Between zero and one times, as many times as possible, giving back as needed (greedy) «?» ' Match the regular expression below «(?:(?!one|two|three|four).?)+» ' Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+» ' Assert that it is impossible to match the regex below starting at this position (negative lookahead) «(?!one|two|three|four)» ' Match either the regular expression below (attempting the next alternative only if this one fails) «one» ' Match the characters “one” literally «one» ' Or match regular expression number 2 below (attempting the next alternative only if this one fails) «two» ' Match the characters “two” literally «two» ' Or match regular expression number 3 below (attempting the next alternative only if this one fails) «three» ' Match the characters “three” literally «three» ' Or match regular expression number 4 below (the entire group fails if this one fails to match) «four» ' Match the characters “four” literally «four» ' Match any single character that is not a line break character «.?» ' Between zero and one times, as many times as possible, giving back as needed (greedy) «?» ' Match the character “/” literally «/?» ' Between zero and one times, as many times as possible, giving back as needed (greedy) «?» ' Assert position at the end of the string (or before the line break at the end of the string, if any) «$» ' Match the characters “ http://somewhere” literally « http://somewhere» ' Match any single character that is not a line break character «.» ' Match the characters “else” literally «else» ' Match any single character that is not a line break character «.» ' Match the characters “com ” literally «com » ' Match a single character present in the list “R=301,L” «[R=301,L]»