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

时间:2011-07-27 23:04:21

标签: apache .htaccess mod-rewrite

我正在努力使用mod_rewrite和.htaccess ...我需要做的就是让我的网址区分大小写。在经历了500次内部服务器错误和大量Google搜索后,我只是在寻找一种可行的解决方案。

工作:Convert to lowercase in a mod_rewrite rule

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

工作:Case Insensitive URLs with mod_rewrite

CheckSpelling on

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

6 个答案:

答案 0 :(得分:4)

以下内容应该有效:

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

如果没有,你能描述一下建议的解决方案没有用吗?

答案 1 :(得分: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页面网址。

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

现在,您可以使用自定义404页面的标准HTML来执行此操作,例如:

<!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时遇到了一些问题,可能是缓存,在开发和测试时,但它现在似乎工作正常。

答案 2 :(得分:1)

来自 user2305090 的上述之一的变体,适用于我:

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

ErrorDocument 404 /forwarding-site-nocase.php

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

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

现在,您可以使用自定义 404 页面的标准 HTML 来执行此操作,例如:

<!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,因为最初建议的版本引发了错误。

答案 3 :(得分: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替换为您网站的网址。将/ path / to / root / dir替换为根目录的绝对路径以开始搜索。使用自定义404错误页面替换404错误,找不到页面。

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

答案 4 :(得分: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

答案 5 :(得分:0)

使用&#34; vhosts.conf&#34;夹。 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]
相关问题