1

I have this code:

location ~* (/.*)?(?<!(food/sweet/dessert))/dollaritems {
  return 301 $1/breakfast/la/dollaritems;
}

This is working fine to redirect users going to any page with /dollaritems in the URL, except a request that includes food/sweet/dessert/dollaritems.

Now, I want to add another negative lookbehind, like this:

location ~* (/.*)?(?<!(food/sweet/dessert))(?<!(food/savory/bacon))/dollaritems {
  return 301 $1/breakfast/la/dollaritems;
}

The second negative lookbehind doesn't seem to be working.

I've also tried:

location ~* (/.*)?(?<!(food/sweet/dessert|food/savory/bacon))/dollaritems {
  return 301 $1/breakfast/la/dollaritems;
}

But that doesn't work either. Is my regex bad here or is not how I should be creating two negative lookbehinds?

Many thanks in advance.

SuperTony
  • 11
  • 1
  • Second negative lookbehind assertion should work, check [this](https://regex101.com/r/G9sIYz/1) (slashes are escaped due to the online checker limitation, you are not required to escape them in the nginx config). Using alternation is also possible (with some limitations), check [this](https://regex101.com/r/o1X2zv/1) one too. – Ivan Shatsky Aug 17 '22 at 22:19
  • That worked. I ended up going with the pipe "|" version. I appreciate you taking the time to create those regex101 examples. I noticed that you removed an extra parenthesis I had in my example on the negative lookback. Do you think that was part of the problem? – SuperTony Aug 19 '22 at 17:15
  • When you are using alternation, an extra parenthesis will result in "A lookbehind assertion has to be fixed width" regex error. In the first example there will be no error with them, but every unused capture group will result in some (small yet non required) performance impact due to the creation of additional variables (`$1`, `$2`, etc.) – Ivan Shatsky Aug 23 '22 at 12:56

0 Answers0