0

Currently i have

server {
    listen 8080;
    server_name default;

    location /foo/ {
        rewrite ^/foo(/.*)$ $1 break;
        proxy_redirect https://example.com https://$host/;
    }

This matches foo and essentially strips it. I need to maintain this functionality, and also include bar if it is after foo. i.e. if the url is /foo/bar I want both of those essentially stripped. I'm not great at regex and am struggling with the proper way to do this.

I'm thinking something like (foo)|(\/bar), but not sure how to match a trailing slash with that as well.

Alex
  • 152
  • 6

1 Answers1

1

Add /bar as an optional component to your regex pattern:

rewrite ^/foo(/bar)?(/.*)$ $2 break;

Note that the capture group reference has been changed into $2

collapsar
  • 17,010
  • 4
  • 35
  • 61
  • Or made the first group a non-capture one: `rewrite ^/foo(?:/bar)?(/.*)$ $1 break;` (should be a little more effective). – Ivan Shatsky Jun 28 '22 at 09:18