6

Currently I have this location block:

location = /events {
    rewrite ^ https://totallydifferenturl.com;
}

This successfully redirects from mywebsite/events, but I want this block to also handle mywebsite/events/.

Trying location = /events/? didn't work out.

0

1 Answer 1

24

You need the ~ operator to enable regex matching, and since you only need to match website/events or website/events/ as full strings, you will need anchors ^ and $ around the pattern:

location ~ ^/events/?$
         ^ ^         ^ 

The ^/events/?$ pattern matches:

  • ^ - start of input
  • /events - a literal substring /events
  • /? - one or zero / symbols
  • $ - end of input.
Sign up to request clarification or add additional context in comments.

2 Comments

This is perfect. What would be the implication if I ignored the anchors and simply used /events/? Would it also match something like website/random/events/query?
Yes, it would match /events or /events/ anywhere inside the string.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.