【问题标题】:Case sensitive URLs - how to make them insensitive?区分大小写的 URL - 如何使它们不区分大小写?
【发布时间】:2011-10-14 16:50:21
【问题描述】:

我在处理 mod_rewrite 和 .htaccess... 我需要做的就是让我的网址不区分大小写。在发生了 500 个内部服务器错误和大量谷歌搜索大量堆栈溢出之后,我只是在寻找一种可行的解决方案。

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

【问题讨论】:

  • 请阅读manualRewriteMap 不能在 .htaccess 中声明——仅在服务器配置或虚拟主机上下文中。否则——规则很好。
  • 嗯……不太明白。我正在使用共享主机,我没有通过 SSH 访问控制台,更不用说服务器配置了......我只想完成这件事:)
  • 简单:如果你把这行RewriteMap tolower int:tolower放到.htaccess中,你会得到500 Server Error。
  • 500 服务器不是我要找的 :) 我宁愿 URL 不区分大小写 :)
  • 嗯——我已经解释了它是如何工作的以及有什么限制。唯一的其他 bullet-proof 选项是将所有请求重定向到某个 .php 文件并在那里执行所有大小写比较/更改逻辑 - 这肯定会起作用。

标签: apache .htaccess mod-rewrite


【解决方案1】:

以下应该有效:

    <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/stuff domain.com - 工作 domain.com/stuff - 500 内部服务器错误 domain.com/Stuff - 显示来自 domain.com 的 index.html、CSS 和未正确加载的图像奇怪吗?
  • @Michal Stefanow Linux 是区分大小写的操作系统——您必须正确键入链接。这就是为什么你会看到几乎每个人都使用小写的文件和文件夹名称——出错的机会更少。
  • 我想让我的网址防弹。 CamelCase 是一种养眼,我想时不时使用它,没有愚蠢的 404 错误页面。
【解决方案2】:

上面的不起作用,但下面的起作用。在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 问题,可能是缓存,但现在似乎可以正常工作了。

【讨论】:

    【解决方案3】:

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

    现在您可以使用您的标准 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,因为最初建议的版本引发了错误。

    【讨论】:

      【解决方案4】:

      把它放在你的 .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 Error, Page Not Found。

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

      【讨论】:

        【解决方案5】:

        (由谷歌翻译)

        递归需要什么?
        如果你知道文件路径:
        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

        【讨论】:

          【解决方案6】:

          使用“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]
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2013-03-06
            • 2012-05-25
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2012-12-01
            • 2020-02-18
            • 2016-06-19
            相关资源
            最近更新 更多