Reputation: 13213
I'm struggling with mod_rewrite and .htaccess... All I need to do is make my URLs case in-sensitive. After couple of 500 internal server errors and a lot of googling a lot of stack overflowing I'm just looking for one working solution.
NOT working: Convert to lowercase in a mod_rewrite rule
RewriteMap tolower int:tolower
RewriteRule ^([^/]+)/?$ somedir/${tolower:$1}
NOT working: Case Insensitive URLs with mod_rewrite
CheckSpelling on
All I need is simple not-case sensitive URLs :)
Upvotes: 14
Views: 33043
Reputation: 189
The following should work:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteMap lowercase int:tolower
RewriteCond $1 [A-Z]
RewriteRule ^/(.*)$ /${lowercase:$1} [R=301,L]
</IfModule>
If not, can you describe what's not working about the proposed solutions?
Upvotes: 5
Reputation: 11
A variation on one of the above from user2305090 which works for me:
First, add the following to your .htaccess file where /forwarding-site-nocase.php is my custom 404 page URL.
ErrorDocument 404 /forwarding-site-nocase.php
Then, at the very top of your custom 404 page, starting on line 1, add the following. If you have a blank line first, it fails.
<?php
$mydir=getdir("/",$_SERVER['REQUEST_URI']);
if($mydir!=false)
{
$thedomain = 'https://' . $_SERVER['SERVER_NAME'];
header("HTTP/1.1 301 Moved Permanently");
header( 'Location: ' . $thedomain.$mydir );
}
function getdir($loc,$tfile)
{
$startloc=$_SERVER['DOCUMENT_ROOT'];
if (file_exists($startloc.$loc) && $handle = opendir($startloc.$loc))
{
while (false !== ($file = readdir($handle)))
{
if ($file != "." && $file != ".." && strncasecmp($loc.$file,$tfile,strlen($loc.$file))==0)
{
if(strncasecmp($loc.$file,$tfile,strlen($tfile))==0)
{
return $loc.$file;
}
else
{
return getdir($loc.$file."/",$tfile);
}
}
}
closedir($handle);
}
return false;
}
?>
Now you can follow this with your standard HTML for your custom 404 page such as:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Language" content="en" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="description" content="Company information page - Page Not Found Error"/>
<meta name="search" content="Company information page - About Us. Manufacturer of rack mount rugged computer systems and rugged LCD displays for the military and industrial markets" />
and so forth for the rest of the page.
Note I changed the "http://" to "https://" and the error file explicitly to .php, since the original suggested version threw an error.
Upvotes: 1
Reputation: 11
using "vhosts.conf" folder. URL's can be made case in sensitive. location : /var/www/vhosts/domain.com/conf/vhosts.conf
RewriteEngine on
rewritemap lowercase int:tolower
RewriteCond $1 [A-Z]
RewriteRule ^/(.*)$ /${lowercase:$1} [R=301,L]
Upvotes: 0
Reputation:
(Translated by Google)
What would be the need to do recursive?
If you know the file path:
dirname ($ _SERVER ['REQUEST_URI'])
It only remains to identify the correct file name.
My simplified version of the script would be:
.htaccess (in root folder)
ErrorDocument 404 /notfound.php
notfound.php (in root folder)
<?php
$tdir=getdir($_SERVER['REQUEST_URI']);
if($tdir != false) {
header("HTTP/1.1 301 Moved Permanently");
header( 'Location: http://www.YOURDOMAIN.COM'.$tdir );
} else {
die('YOUR PERSONAL MESSAGE FOR 404');
}
function getdir($tfile) {
$startloc="/HOME/YOUR/PATH/TO/WWW".dirname($tfile);
if (file_exists($startloc) && $handle = opendir($startloc)) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != ".." && strcasecmp($file,basename($tfile))==0) {
return dirname($tfile).DIRECTORY_SEPARATOR.$file;
}
}
closedir($handle);
}
return false;
}
?>
Remember to change:
1. YOURDOMAIN.COM
2. YOUR PERSONAL MESSAGE FOR 404
3. /HOME/YOUR/PATH/TO/WWW
Upvotes: 0
Reputation: 11
The above doesn't work but the following does. Play with it at www.chassis-plans.com/off-the-shelf-industrial-pc.html. Change any of the case in the URL and the code will change it to the appropriate case and display the page. If you put in a URL that doesn't exist (www.chassis-plans.com/x), the custom 404 page displays.
First, add the following to your .htaccess file where /forwarding-site-nocase.html is my custom 404 page URL.
AddType application/x-httpd-php .html .htm
ErrorDocument 404 /forwarding-site-nocase.html
Then, at the very top of your custom 404 page, starting on line 1, add the following. If you have a blank line first, it fails.
<?php
$mydir=getdir("/",$_SERVER['REQUEST_URI']);
if($mydir!=false)
{
$thedomain = 'http://' . $_SERVER['SERVER_NAME'];
header("HTTP/1.1 301 Moved Permanently");
header( 'Location: ' . $thedomain.$mydir );
}
function getdir($loc,$tfile)
{
$startloc=$_SERVER['DOCUMENT_ROOT'];
if (file_exists($startloc.$loc) && $handle = opendir($startloc.$loc))
{
while (false !== ($file = readdir($handle)))
{
if ($file != "." && $file != ".." && strncasecmp($loc.$file,$tfile,strlen($loc.$file))==0)
{
if(strncasecmp($loc.$file,$tfile,strlen($tfile))==0)
{
return $loc.$file;
}
else
{
return getdir($loc.$file."/",$tfile);
}
}
}
closedir($handle);
}
return false;
}
?>
Now you can follow this with your standard HTML for your custom 404 page such as:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Language" content="en" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="description" content="Company information page - Page Not Found Error"/>
<meta name="search" content="Company information page - About Us. Manufacturer of rack mount rugged computer systems and rugged LCD displays for the military and industrial markets" />
and so forth for the rest of the page.
I had some trouble with IE, probably the cache, when developing and testing but it now seems to work OK.
Upvotes: 1
Reputation: 1468
Put this in your .htaccess file:
ErrorDocument 404 /notfound.html
AddType application/x-httpd-php .html
Put this in notfound.html:
<?php
$tdir=getdir("/",$_SERVER['REQUEST_URI']);
if($tdir!=false)
{
header("HTTP/1.1 301 Moved Permanently");
header( 'Location: http://yourdomain.com'.$tdir );
}
function getdir($loc,$tfile)
{
$startloc="/path/to/root/dir";
if (file_exists($startloc.$loc) && $handle = opendir($startloc.$loc))
{
while (false !== ($file = readdir($handle)))
{
if ($file != "." && $file != ".." && strncasecmp($loc.$file,$tfile,strlen($loc.$file))==0)
{
if(strncasecmp($loc.$file,$tfile,strlen($tfile))==0)
{
return $loc.$file;
}
else
{
return getdir($loc.$file."/",$tfile);
}
}
}
closedir($handle);
}
return false;
}
?>
404 Error, Page Not Found
Replace yourdomain.com with the url of your website. Replace /path/to/root/dir with the absolute path to your root directory to start searching from. Replace 404 Error, Page Not Found with a custom 404 error page.
This script should then recursively search your filesystem for pages with the same name but different capitalization and redirect the user if any are found.
Upvotes: 0