I'd appreciate some help with crafting the right regex for an nginx map block please. Essentially, I need a match a pattern [typically a word: such as course] in a url but url must not end in extensions such as jpg, png, gif

so while it should match:


it should not match:

  • /course/history.png
  • /course.jpg
  • /course/history/student/boy.png

I simply can't get any thing to work, feels like I'm far from it

map $request_uri $redirect_uri_course {
~/course/+ !.(jpg|png|gif)$   http://www.example.com/courses;

Please help, Thanks


You could use a regular expression with a negative lookbehind.

For example:


Alternatively, considering that regular expressions are evaluated in order, you could use two statements, with the first evaluating to zero.

For example:

~\.(jpg|png|gif)([?]|$)    0;
~^/course/               /some/path;
  • many thanks. That seemed to have worked, The second option suits my use case better. Thanks again – pi. May 24 at 8:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.