nginx redirect without trailing slashes AND case-insensitive

Gadjex asked:

I’m still pretty new to nginx and I came up with this example that works to redirect “domain.com/gis” and “domain.com/gis/” to “domain.com/gis_public”. The problem I have is it will not redirect “domain.com/GIS” or “domain.com/Gis”.

I like to plan for all scenarios just in case someone types the GIS acronym in it’s proper uppercase format or only capitalizes a single character. So I need to make this case-insensitive and I have failed to find a way. Any help will be appreciated.

location ~* ^/gis(?:/(.*))?$ {
  rewrite /gis(.*) https://domain.com/gis_public$1 permanent;
}

Edit
My final code that works:

location ~* ^/gis(?:/(.*))?$ {
  rewrite (?i)^/gis(.*) https://domain.com/gis_public$1 permanent;
}

Thanks everyone for your help!

My answer:


It’s just a regex, so you could match on that basis.

rewrite /[Gg][Ii][Ss](.*) https://....

There’s probably a cleaner way to do this, but it doesn’t come to mind right now.


View the full question and any other answers on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.