大小写敏感的URL - 如何使它们不区分大小写?

13

我正在苦苦挣扎于mod_rewrite和.htaccess... 我所需做的只是使我的URLs 不区分大小写。经过多次500内部服务器错误和大量Google和堆栈溢出,我只是在寻找一个可行的解决方案。

不可行的: 将mod_rewrite规则中的内容转换为小写

RewriteMap tolower int:tolower
RewriteRule  ^([^/]+)/?$  somedir/${tolower:$1}

不能工作:使用mod_rewrite实现不区分大小写的URL

CheckSpelling on

我需要的只是简单的不区分大小写的URL :)


1
请阅读手册RewriteMap只能在服务器配置或虚拟主机上下文中声明,不能在.htaccess中声明。否则,规则是正确的。 - LazyOne
嗯...我真的不太明白。我正在使用共享主机,没有通过SSH访问控制台,更不用说服务器配置了...我只是想把这件事情做好 :) - Mars Robertson
简单易懂:如果您将这行代码 RewriteMap tolower int:tolower 放入 .htaccess 文件中,您将会得到 500 服务器错误。 - LazyOne
500服务器不是我想要的 :) 我更希望URL不区分大小写 :) - Mars Robertson
1
好的——我已经解释了它是如何工作以及有哪些限制。唯一其他防弹选项是将所有请求重定向到某个.php文件,并在那里执行所有大小写比较/更改逻辑——这肯定会起作用。 - LazyOne
6个回答

5
以下内容应该可以正常工作:
    <IfModule mod_rewrite.c>
    RewriteEngine on
    rewritemap lowercase int:tolower
    RewriteCond $1 [A-Z]
    RewriteRule ^/(.*)$ /${lowercase:$1} [R=301,L] 
    </IfModule>

如果不行的话,你能描述一下提出的解决方案中哪些不可行吗?

我有domain.com和domain.com/stuffdomain.com - 正常工作domain.com/stuff - 500内部服务器错误domain.com/Stuff - 显示来自domain.com的index.html,CSS和图片未正确加载奇怪吗? - Mars Robertson
@Michal Stefanow Linux是一个区分大小写的操作系统--您必须正确键入链接。这就是为什么你会发现几乎每个人都使用所有小写的文件和文件夹名称--减少犯错的机会。 - LazyOne
3
我希望您能将我的URL变得更加健壮。驼峰式命名法很好看,我想有时候使用它,而不会出现愚蠢的404错误页面。 - Mars Robertson

1

以下是用户user2305090提供的一种变体,适用于我:

首先,在您的.htaccess文件中添加以下内容,其中/forwarding-site-nocase.php是我的自定义404页面URL。

ErrorDocument 404 /forwarding-site-nocase.php

然后,在您的自定义404页面的顶部,从第一行开始,添加以下内容。如果先有一个空白行,则会失败。

<?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;
}
?>

现在您可以按照标准HTML编写自定义404页面,例如:
<!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" />

接下来的页面也是如此。

请注意,我将“http://”更改为“https://”,并明确将错误文件更改为.php,因为原始建议版本会引发错误。


1
上述方法无效,但以下方法有效。请在www.chassis-plans.com/off-the-shelf-industrial-pc.html处尝试。更改URL中的任何大小写字母,代码将更改为相应的大小写并显示页面。如果输入不存在的URL(www.chassis-plans.com/x),则会显示自定义404页面。
首先,在.htaccess文件中添加以下内容,其中/ forwarding-site-nocase.html是我自定义的404页面URL。
AddType application/x-httpd-php .html .htm
ErrorDocument 404 /forwarding-site-nocase.html

然后,在您的自定义404页面顶部,从第1行开始,添加以下内容。如果首先有空白行,则失败。

<?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;
}
?>

现在,您可以按照标准的HTML编写自定义404页面,例如:
<!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" />

接下来的页面内容类似,具体请参考。

在开发和测试时,我遇到了一些IE浏览器的问题,可能与缓存有关,但现在看起来已经正常工作了。


0

使用 "vhosts.conf" 文件夹。URL 可以不区分大小写。位置:/var/www/vhosts/domain.com/conf/vhosts.conf

RewriteEngine on
rewritemap lowercase int:tolower
RewriteCond $1 [A-Z]
RewriteRule ^/(.*)$ /${lowercase:$1} [R=301,L]

0
将以下内容放置在您的 .htaccess 文件中:
ErrorDocument 404 /notfound.html
AddType application/x-httpd-php .html

将此内容放入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

将yourdomain.com替换为您网站的url。 将/path/to/root/dir替换为您的根目录的绝对路径以开始搜索。 将404错误,页面未找到替换为自定义404错误页面。

然后,此脚本应递归地搜索文件系统,查找具有相同名称但大小写不同的页面,并在找到任何页面时重定向用户。


0

递归的必要性是什么?
如果您知道文件路径:
dirname($_SERVER['REQUEST_URI'])

只需确定正确的文件名即可。

我简化版本的脚本如下:

.htaccess(在根目录中)
ErrorDocument 404 /notfound.php

notfound.php(在根目录中)

<?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;
}
?>

记得更改:
1. YOURDOMAIN.COM
2. 404 页面的个人信息
3. /HOME/YOUR/PATH/TO/WWW 的路径


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接