13

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 :)

5
  • 1
    Read the manual please: RewriteMap CANNOT be declared in .htaccess -- only in server config or virtual host context. Otherwise -- the rule is fine.
    – LazyOne
    Jul 27, 2011 at 23:19
  • HMMM... Don't really understand. I'm using shared hosting and I don't have console access via SSH, not to mention server config... I just want to get this done :) Jul 29, 2011 at 6:41
  • Easy: if you put this line RewriteMap tolower int:tolower into .htaccess, you will get 500 Server Error.
    – LazyOne
    Jul 29, 2011 at 7:24
  • 500 Server is not what I'm looking for :) I would rather have URLs not case sensitive :) Aug 4, 2011 at 13:18
  • 1
    Well -- I've already explained how it works and what are the limitations. The ONLY other bullet-proof option is to redirect ALL requests to some .php file and do all the case-comparison/changing logic there -- this will work for sure.
    – LazyOne
    Aug 4, 2011 at 13:30

6 Answers 6

5

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?

3
  • I have domain.com and domain.com/stuff domain.com - works domain.com/stuff - 500 internal server error domain.com/Stuff - shows index.html from domain.com, CSS and images not loaded correctly Weird? Jul 29, 2011 at 6:42
  • @Michal Stefanow Linux is case-sensitive OS -- you have to type your links properly. That is why you will see that almost everyone uses all lower case names for files and folders -- less chances to make a mistake.
    – LazyOne
    Jul 29, 2011 at 7:23
  • 3
    I would like to make my URLs bullet-proof. CamelCase is an eye candy and I would like to use it from time to time, without silly 404 error pages. Aug 1, 2011 at 10:41
1

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.

1

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.

0

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.

0

(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

0

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]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.