Reputation: 310
I'm trying to redirect Capital Case file names only to a lowercase file on the server, retaining intact directories case.
I don't have access to httpd.conf.
I have found this that do the trick with php and RewriteRule but in my case I'm unable to capture the directories between $_SERVER['HTTP_HOST']/base/
and filename
, so I lost them in the redirection causing an apache fail.
The rules are:
RewriteEngine on
RewriteBase /
# 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 (?=([^\/])*(\.htm|\.html|\.jpg))(.*) rewrite-strtolower.php?rewrite-strtolower-url=$3 [QSA,L]
The RegEx (?=([^\/])*(\.htm|\.html|\.jpg))(.*)
captures and passes only filename to the php script who makes the real redirect:
<?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'] . '/base/' . 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.');
Example of URL I need to test is:
http://test.com/base/some Folder/some Other subfolder/Some-File-Name.jpg
Needed Result:
http://test.com/base/some Folder/some Other subfolder/some-file-name.jpg
Is there a way to correct things to capture the part of URL that I'm throwing out?
Edit: My entire .htaccess is visible here.
Upvotes: 0
Views: 866
Reputation: 24468
Don't use the exact example. You can get the full path in PHP as well. You don't need to capture the URI. You can use REQUEST_URI
in PHP to get the full path. I would do something like this.
Just send the full request to php and then handle it there.
RewriteEngine on
RewriteBase /
#don't do anything if php file is directly requested.
RewriteRule ^rewrite-strtolower\.php - [L]
# 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 [L]
//php file
<?php
if(isset($_SERVER["REQUEST_URI"])){
$URI = $_SERVER["REQUEST_URI"];
$URI = ltrim($URI,"/");
$parts = explode("/",$URI);
$filename = array_pop($parts);
$URI = implode("/",$parts);
header('Location: http://' . $_SERVER['HTTP_HOST'] .'/'.$URI.'/'.strtolower($filename), true, 301);
exit;
}
?>
Upvotes: 1