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).

THANKS
Request for Proposal

Recaptcha is required.

Sending message..