代码之家  ›  专栏  ›  技术社区  ›  Ian Jamieson

将子域重定向到文件夹时出现问题

  •  0
  • Ian Jamieson  · 技术社区  · 13 年前

    我有这样的工作:

    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^([^\.]+)\.domain\.co\.uk
    RewriteRule ^(.*) http://domain.co.uk/%1 [L]
    

    因此,它将把test.domain.co.uk重定向到test.domainco.uk/test

    但是,我希望它能更改文档根,而不是让用户看到重定向。我在这里读了几篇文章,并尝试了一些事情。但我不知道如何调试任何错误,它就是不起作用。我试过的另一个:

    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_URI} !^/-
    RewriteCond %{HTTP_HOST} ^([^\./]+)
    RewriteCond %{DOCUMENT_ROOT}/-%1 -d
    RewriteRule ^(.*)$ -%1/$1 [L] 
    

    但这似乎根本不起作用。

    如有任何帮助,我们将不胜感激。

    伊恩

    1 回复  |  直到 13 年前
        1
  •  1
  •   Jon Lin    13 年前

    在文档根目录中尝试以下操作:

    RewriteEngine On
    
    # check if subdomain folder has the existing file, if so, serve it
    RewriteCond %{HTTP_HOST} ^([^\.]+)\.domain\.co\.uk
    RewriteCond %{DOCUMENT_ROOT}/%1%{REQUEST_URI} -f
    RewriteRule ^(.*)$ /%1/$1 [L] 
    
    # check if subdomain folder has the existing directory WITH A TRAILING SLASH, if so serve it
    RewriteCond %{HTTP_HOST} ^([^\.]+)\.domain\.co\.uk
    RewriteCond %{DOCUMENT_ROOT}/%1%{REQUEST_URI} -d
    RewriteRule ^(.*?)/$ /%1/$1/ [L] 
    
    # check if subdomain filder has the existing directory but missing trailing slash, redirect with it
    RewriteCond %{REQUEST_URI} !/$
    RewriteCond %{HTTP_HOST} ^([^\.]+)\.domain\.co\.uk
    RewriteCond %{DOCUMENT_ROOT}/%1%{REQUEST_URI} -d
    RewriteRule ^(.*)$ /$1/ [R=301,L] 
    

    这个方法为你做了两件事,如果它是404,它不会尝试内部重写,所以如果你去: http://sub.domain.co.uk/this/path/does/not/exist/blah ,您不会收到一条404消息: /sub/this/path/does/not/exist/blah 不存在,只是 /this/path/does/not/exist/blah 因为URI没有被重写,因此没有暴露您的底层目录结构。

    第二件事是,你可能有 DirectorySlash 已打开。这意味着如果您访问以下目录: http://sub.domain.co.uk/this/path ,缺少尾部斜杠,mod_dir将希望重定向并添加该斜杠,但它可能会出错,因为URI已被重写,并将您重定向到: http://sub.domain.co.uk/sub/this/path/ 。我们预先添加了带有正确URI的尾部斜杠,以隐藏 sub