0

This is my first post ever. Hopefully, I'll be able to explain my problem thouroughly.

I have an Nginx config that redirects my.domain.com/avocado to my.domain.com/avocado/ (with a slash in the end). This also redirect subpaths (end with a slash) after the /avocado path like:

my.domain.com/avocado/banana --> my.domain.com/avocado/banana/ my.domain.com/avocado/banana/apple --> my.domain.com/avocado/banana/apple/

Now, we noticed that URLs with "/--" as the last subpath (example: my.domain.com/avocado/banana/apple/--) is not redirecting correctly. It should redirect to my.domain.com/avocado/banana/apple/ (append a slash to the last subpath)

I think this is regex stuff too but I have no idea how to correctly do it. Would you guys suggest any regex? Thank you!

server {
  server_name my.domain.com;
  location /avocado {
    resolver 10.1.0.2 valid=60s;
    if ($http_x_forwarded_proto != "https") {
       rewrite ^(.*)$ https://$server_name$1 permanent;
    }
    rewrite ^([^.]*[^/])$ https://$server_name$1/ permanent;
    proxy_set_header X-Real-IP  $remote_addr;
    proxy_set_header X-Forwarded-For $remote_addr;
    proxy_set_header Host $host;
    proxy_pass http://$upstreamserver$request_uri;
}

}

1 Answers1

1

It's your first question and my first answer then.

I see two ways to do this.

  1. ^([^.]*)(?:(?<!/|--)|/--)$
    • ^([^.]*) from the start of the line, capture everything that is not a dot as many times as possible, giving back as needed.
    • (?:(?<!/|--)|/--)$ captures the end of the line when there isn't a / or /-- before it, or the /-- at the end of the line if it exists, then replaces them with /.
    • Regex101
  2. ^([^.]*?)(?:/--)?(?<!/)$
    • ^([^.]*?) from the start of the line, capture everything that is not a dot as few times as possible, expanding as needed.
    • (?:/--)? match /-- if it exsists but don't capture it.
    • (?<!/)$ match if the end of the line isn't preceeded by /.
    • Regex101
Omar Si
  • 154
  • 1
  • 2
  • 5