nginx rewrite url to lowercase url
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.
Related videos on Youtube
thames
Updated on September 18, 2022Comments
-
thames almost 2 years
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.
-
cyberx86 about 12 yearsOne option is to use an additional location block that matches any upper case characters, and contains a rewrite to the lowercase uri. You can perform the string manipulation needed using the embedded perl module (e.g. like this)
-
thames about 12 yearsYeah, I'm trying to make it easier for dev's in how they currently work and having to not rename a bunch of files and directories. Basically a uri of /Home to do a permanent redirect to /home, but have the /home match the physical /Home directory? Make sense? Kind of stupid, but going for SEO and moving from MSFT website to Linux. From what you've provided (I'll double check) it looks like uri of /home will not match the physical dir /Home