Convert url from uppercase to lowercase

Convert url from uppercase to lowercase

First put this below code in .htaccess,

# force url to lowercase if upper case is found
RewriteCond %{REQUEST_URI} [A-Z]
# ensure it is not a file on the drive first
RewriteCond %{REQUEST_FILENAME} !-s
RewriteRule (.*) rewrite-strtolower.php?rewrite-strtolower-url=$1 [QSA,L]

Then add a newfile in the same directory of .htaccess file. and put the below contents to it.

<?php
if(isset($_GET['rewrite-strtolower-url'])) {
$url = $_GET['rewrite-strtolower-url'];
unset($_GET['rewrite-strtolower-url']);
$params = http_build_query($_GET);
if(strlen($params)) {
$params = '?' . $params;
}
header('Location: http://' . $_SERVER['HTTP_HOST'] . '/' . strtolower($url) . $params, true, 301);
exit;
}
header("HTTP/1.0 404 Not Found");
die('Unable to convert the URL to lowercase. You must supply a URL to work upon.');

Orelse try this,

Add this in httpd.conf file,

RewriteMap lower int:tolower

And this to .htaccess file,

RewriteRule [A-Z] ${lower:%{REQUEST_URI}} [R=302,L]

This will fix the issue.

Leave a comment