11

I'm trying to match /category/anything, except /category/paid in nginx location.

I have the following regex, but it's not working. Google tells me that I can use lookahead in nginx. Am I doing something wrong?

location ^/category(?!/paid)/ {

}
Moon
  • 22,195
  • 68
  • 188
  • 269

1 Answers1

11

You either need a slash before it or an escaped slash.

location ~ (category/(?!paid)) { .. }
location ~ (category\/(?!paid)) { .. }
buley
  • 28,032
  • 17
  • 85
  • 106
hwnd
  • 69,796
  • 4
  • 95
  • 132
  • That's exactly what I was looking for! Thanks! one more question..looks like my regex doesn't match /category/paid-test. I've added $ at the end, but it still doesn't match. Do you have any idea? – Moon Sep 04 '13 at 01:47
  • 1
    Maybe something like `category/(?:paid(?=-)|(?!paid))` – hwnd Sep 04 '13 at 02:24
  • 4
    I dont understand what's the difference between `^/category(?!/paid)/` and `(category/(?!paid))` both should be equivalent and work for the example (match /category/anything, except /category/paid). What am I missing there? – Freefri Oct 26 '17 at 11:51
  • this (category/(?!paid)) not matches paid prefix not a directory only. these are bad: category/paid21398410 category/paid/ – Shimon Doodkin Aug 02 '20 at 07:18