Nginx: Redirect all Uppercase URLs to Lowercase URLs
Posted By : Prakhar Budholiya | 28-Mar-2016
I wanted to make all my nginx URLs to be served in Lowercase only, Initially my nginx server was case insensitive and was serving both Uppercase and Lowercase URLs. But From SEO point of view, we needed to Server only in Small URLs
Ex- My website runs on both the URLs - www.example.com/about-us & www.example.com/ABOUT-uS .. etc.
So what i needed to do was redirect all Uppercase urls to Lowercase, i.e. if a User hits www.example.com/ABOUT-us or www.example.com/aBoUt-Us, he will be redirected to www.example.com/about-us .
So Here is What you need to do -
First of all we need to have perl install on our server, Install it as
# apt-get install nginx-extras
# vim /etc/nginx/nginx.conf
(here go to section http {}, and in betwwen these braces write )
http {
perl_modules perl/lib;
perl_set $uri_lowercase ‘sub {
my $r = shift;
my $uri = $r->uri;
$uri = lc($uri);
return $uri;
}’;
Save and exit
# vim /etc/nginx/sites-available/default
location ~ [A-Z] {
rewrite ^(.*)$ $scheme://$host#uri_lowercase;
}
Save and exit
Note- Now all your Uppercase urls will be redirected to lowercase, So make sure that you don't have any URL which is case sensitive (i.e. which do not serve in lowercase).
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Prakhar Budholiya
Prakhar is a linux administrator and well versed with linux and networking. he loves horse riding and is an athlete.