Convert Subcontext of a Web URL to lowercase using Apache Mod_Rewrite

This article focus on a requirement from a customer who wants to alter a Web URL to his website. He wanted to convert a section in the request URL to lowercase. In a nutshell, below show his requirement.

Original: http://www.example.org/App/dosomething.php
Alterd: http://www.example.org/app/dosomething.php

As you can see, he only wants the subcontext changed to lowercase letters. Since he web server was Apache, solution was to use Apache Mod_Rewrite.

Therefore, the solution is to use Apache RewriteMap as shown in the below code snippet.

RewriteEngine On
RewriteMap lowercase int:tolower
RewriteCond $1 [A-Z]
RewriteRule ^/([^/]+)/(.*) http://www.example.org/${lowercase:$1}/$2 [R=301,L]

Finally, make sure you reload the Apache service after adding above code.

Leave a Reply

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