Created
October 6, 2010 14:20
-
-
Save treffynnon/613413 to your computer and use it in GitHub Desktop.
Force URLs to lower case in a RewriteRule using PHP. Blogged at http://simonholywell.com/post/2012/11/force-lowercase-urls-rewrite-php.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
RewriteEngine on | |
RewriteBase / | |
# force url to lowercase | |
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); | |
} else { | |
header("HTTP/1.0 404 Not Found"); | |
die('Unable to convert the URL to lowercase. You must supply a URL to work upon.'); | |
} | |
exit(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment