Я сейчас переношу установку с 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/
не будет, поскольку в нем есть последовательность один в нем: / категория *** one *** ries / категория-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]»