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 “” and “” to “”. The problem I have is it will not redirect “” or “”.

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(.*)$1 permanent;

My final code that works:

location ~* ^/gis(?:/(.*))?$ {
  rewrite (?i)^/gis(.*)$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 answer on Server Fault.

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