Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

nginx

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

something is nginx location (/something) directory

Please suggest how to implement case insensitive directory location redirection

like image 948
Santosh Prasad Avatar asked Oct 27 '14 12:10

Santosh Prasad


2 Answers

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;
}
like image 153
Cole Tierney Avatar answered Nov 15 '22 16:11

Cole Tierney


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
}
like image 38
zavg Avatar answered Nov 15 '22 15:11

zavg