Nginx rewrite url to lowercase url

fastcgimononginxrewrite

I have nginx setup, however I'm needing to fix an issue where someone types "/Home" to:

  • rewrite it to the lowercase "/home" (do this to all files and directories, not just home) but locate the Uppercase (if there is one) file.
  • Or do I need to add another location that uses the case-insensitive characters "~*" ?

The actual directory and file layout does contain mixed Upper and lower case characters. I would like to preserve this for simplicity sake, i.e. /Home and /About are the actual directories, but for SEO I want to permanent redirect to the lowercase /home but find the actual /Home directory.

I'm not sure if this can be accomplished by a rewrite rule or if there is a case insensitivity on the "location / { }" or some combination.


        location / {
                try_files $uri $uri/ /Home;
        }

Note: I'm not looking for answers that fall into Nginx Pitfalls, including the evil Ifs.

Best Answer

As mentioned in comments to your question, this is possible by use of perl module. Assuming you'll use that $uri_lowercase you may write something like this:

location / {
    try_files $uri $uri/ $uri_lowercase $uri_lowercase/ =404;
}

Note however that $uri_lowercase may not fit your needs if you have mixed case URLs like /Home/UseR/teSt.html and you need to try all bunch of partially lowercased uri. If this is your case i strongly advice to get some policy on your directories and require them to be lowercase.