Jerry Jones February 2016

Case insensitivity in nginx

We have several SEO pages like:

http://www.example.com/PageOne.html

Which we redirect in config like:

location = /PageOne.html {
  rewrite ^/(.*) /seo.php?id=1 last;
}

Problem is if a user access this page by typing:

http://www.example.com/pageone.html

"Page Not Found" error is displaying. There are approximate 500+ seo pages. How to write rule for nginx to ignore case sensitivity in url? I want a common solution for all url.

Answers


CDub February 2016

Specifically for PageOne.html, you can do the following:

location ~ /PageOne.html {
    return 301 http://www.example.com/pageone.html$1;
}

If you have multiple URIs which need to be redirected, it appears the best option is to use Perl:

location ~ [A-Z] {
  perl 'sub { my $r = shift; $r->internal_redirect(lc($r->uri)); }';
}

If you have hundreds of unique URIs which would involve many location blocks as above, I'd consider changing your application to handle lowercase URIs rather than expecting the webserver to handle the lowercase conversion.


Jerry Jones February 2016

This solved my issue. Sad to say that there is not many articles related to these issues, even nginx doesn't provide user friendly Help/Tutorials.

        location ~* ^/-PageOne.html {
             rewrite ^ /seo.php?page_id=1 last;
        }

Hope this helps!

Post Status

Asked in February 2016
Viewed 2,650 times
Voted 6
Answered 2 times

Search




Leave an answer