【问题标题】:Rewrite subdomain to subfolder causes loop error or 403将子域重写到子文件夹导致循环错误或 403
【发布时间】:2013-05-06 19:51:44
【问题描述】:

我在我的机器上设置了一个本地 Apache 服务器,并使用了通配符 DNS。我已将其设置为像[foldername].loc 一样工作。因此,例如,我的htdocs 文件夹下名为MyDomain 的文件夹将通过mydomain.loc 访问。此代码工作正常,我的.htaccess 中的代码在我的htdocs 中如下:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?([a-z0-9-]*\.)?([a-z0-9-]+)\.loc$ [NC]
RewriteCond %3::%{REQUEST_URI} !^(.*?)::/\1/?
RewriteRule (.*) /%3/$1 [PT,QSA]

现在,上面的代码还通过子域,例如“john.mydomain.loc”。现在,我在文件夹MyDomain 中有以下文件夹结构:

MyDomain
    - active
        - index.php
    - working
        - index.php
    .htaccess

MyDomain.htaccess中有如下代码:

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^live\.mydomain\.loc$ [NC]
RewriteRule ^(.*)$ /active/$1 [L]

如果我理解正确的话,这应该是把http://live.mydomain.loc/ 改写为http://mydomain.loc/active/。请注意,我说的是 rewrite,而不是 redirect

但是,使用上面的代码,我在 Apache 错误日志中收到一条消息:

[client 127.0.0.1] Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace.

如果我将MyDomain.htaccess 更改为如下所示:

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^live\.mydomain\.loc$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /active/$1 [L]

当我使用此代码时,它总是出现 403 错误,说我无权查看文件夹 /mydomain/。如果我设置Options +Indexes,我只看到/mydomain的文件夹索引。那么上面的代码在哪里失败了?

我也用RewriteCond %{REQUEST_URI} !^/active/ 尝试了上面的代码。这对结果没有影响。

我已经尝试了两天多,但我无法弄清楚。我希望 StackOverflow 的聪明才智可以帮助解决这个问题。 :)

【问题讨论】:

    标签: .htaccess mod-rewrite subdomain wildcard-subdomain


    【解决方案1】:

    尝试将重写基础设置为文件实际所在的位置:

    RewriteEngine On
    RewriteBase /MyDomain/
    RewriteCond %{HTTP_HOST} ^live\.mydomain\.loc$ [NC]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ active/$1 [L]
    

    使/active/ 相对:active/

    【讨论】:

    • 谢谢,虽然我觉得我们走在正确的轨道上,但效果并不好。这是我重写日志文件的摘录:internal redirect with /mydomain/active/active/active/active/active/active/active/active/active/active/ [INTERNAL REDIRECT] 这是日志的最后一行,显然我们在某处遗漏了一个循环点。有什么想法吗?
    • 我想通了。我已将其作为答案提交。谢谢您的帮助。 :)
    【解决方案2】:

    我使用下面的代码解决了这个问题:

    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^live\.mydomain\.loc$ [NC]
    RewriteCond %{REQUEST_URI} !^/mydomain/active/
    RewriteRule ^(.*)$ active/$1 [L]
    

    主要修复是RewriteCond %{REQUEST_URI} !^/mydomain/active/。不同之处在于,我尝试的第一行 RewriteCond %{REQUEST_URI} !^/active 将匹配 /access/index.php,但不匹配 /mydomain/active/index.php

    我希望这对其他人有帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-12-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-31
      • 2023-03-27
      • 2011-03-02
      • 2014-06-30
      相关资源
      最近更新 更多