How to Use .htaccess to Make URLs Case Insensitive


.htaccess to make urls case insensitive

If your server is case sensitive then users might find it difficult to reach some of the pages. Little differences in URL case will give “404 : page not found” error and it will be a huge turn off for visitors. Luckily, you can use .htaccess to make URLs case insensitive in Apache web server.

 

Before you proceed, please ensure you have installed Apache and enabled mod_rewrite for your server.

  1. How to enable mod_rewrite for Apache on CentOS
  2. How to enable mod_rewrite for Apache on Ubuntu

 

How to Use .htaccess to Make URLs Case Insensitive

 

For a single URL

If you just need to make one URL (e.g /Pay) case insensitive (e.g /pay), you can paste the following command in your .htaccess file


RewriteRule ^Pay ./pay.php [L] #Pay

 

 

Using mod_spelling module

Apache provides a mod_spelling module that allows Apache to do spell checks and case checks on URLs to find a match. Enable mod_spelling module and paste the following lines in your .htaccess file.


<IfModule mod_speling.c>
 CheckCaseOnly On
 CheckSpelling On
</IfModule>

 

Using Rewrite rule

This solution works if all your URLs are lower cased. Here mod_rewrite will force all URLs to be lower case.


RewriteMap tolowercase int:tolower
RewriteRule ^(.*)$ ${tolowercase:$1}

 

Using Virtual Host and .htaccess

By default, you’ll find Ubuntu Apache configuration file at /etc/apache2/sites-enabled/your_domain

If you are unable to find it at that location, you can find it with the command


$ sudo a2ensite your_domain

 

Open server config file and place the following line in it


RewriteMap lc int:tolower

 

Place the following line in the .htaccess file


RewriteCond %{REQUEST_URI} [A-Z]
RewriteRule . ${lc:%{REQUEST_URI}} [R=301,L]

This will redirect any URL that has at least 1 capital letter in it (except the query string), to the same but lower case version.

 

If you want to restrict it to a specific file type (e.g .html), then you can use the following command


RewriteCond %{REQUEST_URI} [A-Z]
RewriteRule ^.+\.html$ ${lc:%{REQUEST_URI}} [NC,R=301,L]

About Sreeram Sreenivasan

Sreeram Sreenivasan is the Founder of Ubiq, a business dashboard & reporting platform for small & medium businesses. Ubiq makes it easy to build business dashboards & reports for your business. Try it for free today!