I'm in the process of migrating a setup from apache to nginx. In this process I've come across this rewrite rule in an .htaccess file:
RewriteRule ^/?(?:(?!one|two|three|four).?)+/?$ http://somewhere.else.com [R=301,L]
I'm pretty good at regex usually, but this is way beyond me. For starters, I didn't know embedded parenthesis were even allowed. Can someone explain this regex to me? And if it's an apache only thing, how I can replicate it in nginx?
(?!one|two|three|four)
means "NOT (one or two or three or four)".The
?:
means non-catching group (so you cannot reference it using$N
, e.g.$1
).All together it pretty much means ANY text that has no "one" or "two" or "three" or "four" sequence in it.
For example:
This URL
/categories/category-1/hello-kitten/
if applied to the above rule, will be redirected. But this one/categoneries/category-1/hello-kitten/
will not, as it has sequence one in it: /categ***one***ries/category-1/hello-kitten/Here's some more specific and detailed info in case it helps: