我正在将一个设置从apache迁移到nginx。 在这个过程中,我在.htaccess文件中遇到了这个重写规则:
RewriteRule ^/?(?:(?!one|two|three|four).?)+/?$ http://somewhere.else.com [R=301,L]
我通常在正则expression式方面很不错,但是这远远超过了我。 对于初学者,我不知道embedded括号甚至被允许。 有人可以向我解释这个正则expression式吗? 如果这只是一个Apache的东西,我怎么可以复制它在Nginx?
(?!one|two|three|four) “三”或“四” (?!one|two|three|four)意思是“不(一或二或三或四)”。
?:表示非捕获组(因此您不能使用$N来引用它,例如$1 )。
所有这一切几乎意味着任何没有“一个”或“两个”或“三个”或“四个”序列的文本。
例如:
这个URL /categories/category-1/hello-kitten/如果应用于上述规则,将被redirect。 但是这个/categoneries/category-1/hello-kitten/不会,因为它有一个序列:/ categ *** one *** ries / 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]»