How to implement Nginx case-insensitive directory-location redirection 301

后端 未结 2 1879
梦谈多话
梦谈多话 2020-12-17 22:19

I want to http://example.com/SomeThing redirect to http://example.com/something

something is nginx location (/s

相关标签:
2条回答
  • 2020-12-17 23:07

    I'm assuming that http://example.com/something would not be redirected. So use a prefix location for the case sensitive match with the ^~ modifier to skip checking regular expressions:

    location ^~ /something {
        return 200 "case sensitive something match
    ";
    }
    

    Now add the case insensitive regular expression location for the redirect:

    location ~* ^/something {
        return 301 $scheme://$host/something;
    }
    
    0 讨论(0)
  • 2020-12-17 23:14

    From nginx docs:

    A location can either be defined by a prefix string, or by a regular expression. Regular expressions are specified with the preceding “~*” modifier (for case-insensitive matching), or the “~” modifier (for case-sensitive matching).

    So ~* in location must be used for case insensitive matching.

    location ~* /something/ {
        # your code here
    }
    
    0 讨论(0)
提交回复
热议问题