I'm new to htaccess.
I need to change all first level directories to uppercase.
If anyone can help, I would greatly appreciate it.
04-16-2009, 11:59 PM
Do you have access to the httpd.conf file or do you only have .htaccess file access?
No, I don't have access to the httpd.conf file.
04-17-2009, 12:53 AM
Then you will need a PHP script as well
RewriteRule ^([^/]*[a-z][^/]*)(/.*)?$ /script.php?dir=$1&rest=$2 [QSA,L]
if(getenv('REDIRECT_STATUS') === false || !isset($_GET['dir'])
|| empty($_GET['dir']) || !isset($_GET['rest']))
# The request was not processed by mod_rewrite.
header('Location: http://' . getenv('HTTP_HOST') . '/', true, 301);
$redirect = strtoupper($_GET['dir']) . $_GET['rest'];
$query_string = getenv('QUERY_STRING');
$redirect .= '?' . $query_string;
header('Location: http://' . getenv('HTTP_HOST') . '/' . $redirect, true, 301);
Thanks for the info but I must be doing something wrong.
I put the script in the root dir & added the above to the htaccess file but it doesn't uppercase the dir.
04-17-2009, 06:54 PM
If you put the following mod_rewrite in your .htaccess file
RewriteRule Google http://www.google.com/? [NC,R,L]
and go to http://example.com/Google, do you get redirected to Google?
04-18-2009, 01:13 AM
If you haven't got htaccess with mod rewrite enabled which it seems you haven't Then the only thing i can think that you can do is have two folders in your document root one with capitals and one without. Which will suck if you have lots of PHP files to redirect from. But if its just one index file might not be so bad. Well will still suck but. And this is the index file you would put in the lower case folder.
$url = $_SERVER["PHP_SELF"];
$url_folders = explode ('/', ltrim($url, "/"));
$url_folders = strtoupper($url_folders);
$url_folders = "/". implode('/', $url_folders);
$qs = "";
$qs = "?". $_SERVER["QUERY_STRING"];
$domain = $_SERVER["SERVER_NAME"];
header('Location: http://' . $domain . $url_folders . $qs);
But again this sucks. It would work on linux server but not on a windows server. Because windows you can't have a lower and upper case version of the folders. Anyway if it was me I would contact your server host and ask about mod rewrite.
04-19-2009, 04:59 PM
You can use a custom 404 handler instead.
ErrorDocument 404 /404handler.php
$request_uri = getenv('REQUEST_URI');
# Does the first directory contain a lower case letter?
if(preg_match('~^/([^/]*[a-z][^/]*)(/(.*))?$', $request_uri, $matches))
# Make the first directory upper case.
$first_directory = strtoupper($matches);
# Does the directory exist?
if(is_dir(getenv('DOCUMENT_ROOT') . '/' . $first_directory))
# Redirect to the upper case directory.
header('Location: http://' . getenv('HTTP_HOST') . '/' . $first_directory . '/' . $matches, true, 301);
Normal 404 page here.
Thanks to everyone for the help.
The 404 redirect worked Great!!!