Menu Close

Change uppercase URI to lowercase with mod_rewrite (.htaccess)

4.7
(6)

Last Updated on August 12, 2021 by Amit

htaccess uppercase to lowercase

This article explains how to change an uppercase URI string to lowercase with mod_rewrite in an .htaccess file.
With htaccess you can easily convert upper-case characters in URL path to lower-case.
With just a few modifications and by adding a few lines of code you can redirect all of your Upper-case paths to lower-case to make the URL look nice with lower-case characters.
On this article we will provide you a 100% working solution that can just be copied and pasted.

Htaccess redirect uppercase characters in URL path to lowercase

A URL path with upper-case (capital latters) looks something like the following :

/PATH

And a path with lower-case characters looks something as shown below :
/PATH

Assuming your server has the following URL format :

https://example.com/THIS_PATH

You want to redirect URLs like this to its lower-case version

https://example.com/this_path

Here is the RewriteRule you can use in your htaccess to achieve this behaviour :


RewriteEngine on
RewriteCond expr "tolower(%{REQUEST_URI}) =~ m#(.+)/?$#"
RewriteRule [A-Z] %1 [R=301,L]

The rule above searches and converts all Capital letters in URL path string to small letter version. The location where you will put this in an htaccess file is important. If you put this at the bottom or before other rules/directives then it will not be read by server. You will need to put this at the top in your htaccess file.
I hope this article was helpful. Thanks for reading it.

How useful was this post?

Click on a star to rate it!

Average rating 4.7 / 5. Vote count: 6

No votes so far! Be the first to rate this post.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

code