奇怪的行为:由于可能的配置错误,请求超出了 10 个内部重定向的限制

Weird behavior: Request exceeded the limit of 10 internal redirects due to probable configuration error

我有这个 Apache 虚拟主机定义:

<VirtualHost *:80>
    ServerName server1.qa.com
    ServerAlias server2.qa.com
    ServerAlias server3.qa.com

    DirectoryIndex app_dev.php

    DocumentRoot /var/www/html/web
    <Directory /var/www/html/web>
        # enable the .htaccess rewrites
        AllowOverride All
        Order allow,deny
        Allow from All
    </Directory>

    ErrorLog /var/log/httpd/error.log

    LogFormat "%h %l %t \"%r\" \"%{X-PDONE-SESSION-ID}i\" %>s %b" common
    CustomLog /var/log/httpd/access.log common
</VirtualHost>

如果我在没有 app_dev.php 的情况下调用 http://server1.qa.com,我将以这个错误结束:

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.

如果我改为调用 http://server1.qa.com/app_dev.php 一切都很好,应用程序可以运行。问题出在哪里?这是一个 Symfony 2.7 项目,我正在尝试为该路径设置 DirectoryIndex,有什么建议吗?

这是 .htaccess 定义:

DirectoryIndex app.php

<IfModule mod_negotiation.c>
    Options -MultiViews
</IfModule>

<IfModule mod_rewrite.c>
    RewriteEngine On

    RewriteCond %{REQUEST_URI}:: ^(/.+)/(.*)::$
    RewriteRule ^(.*) - [E=BASE:%1]

    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

    RewriteCond %{ENV:REDIRECT_STATUS} ^$
    RewriteRule ^app\.php(/(.*)|$) %{ENV:BASE}/ [R=301,L]

    RewriteCond %{REQUEST_FILENAME} -f
    RewriteRule .? - [L]

    RewriteRule .? %{ENV:BASE}/app.php [L]
</IfModule>

access.log

access.log 中没有任何奇怪或有用的内容,如以下行所示:

// called server1.qa.com

[04/Sep/2015:12:25:09 -0400] "GET / HTTP/1.1" 500 618

// called server1.qa.com/app_dev.php
[04/Sep/2015:12:25:14 -0400] "GET /app_dev.php HTTP/1.1" 302 340
[04/Sep/2015:12:25:14 -0400] "GET /app_dev.php/admin/login HTTP/1.1" 302 416
[04/Sep/2015:12:25:15 -0400] "GET /app_dev.php/login HTTP/1.1" 200 21521
[04/Sep/2015:12:25:16 -0400] "GET /app_dev.php/_wdt/c84bab HTTP/1.1" 200 40961

好的,我再试试

我认为拥有两个 .htaccess 文件会更容易,每个环境一个:

  • 从虚拟主机文件中删除 DirectoryIndex
  • 将您的 .htaccess 复制到 .htaccess_dev
  • 将.htaccess_dev中的全部"app.php"替换为"app_dev.php"
  • 在您的虚拟主机中添加 "AccessFileName .htaccess_dev" 并重新启动您的 apache。

现在,您的 .htaccess_dev 将被使用,它不应触及您的 app.php 文件。

您设置了虚拟主机,因此 app_dev.php 是索引,但在您的 htaccess 文件中,它使用 app.php。尝试改为使用 app_dev.php

# don't really need this, it's in your vhost
#DirectoryIndex app_dev.php

<IfModule mod_negotiation.c>
    Options -MultiViews
</IfModule>

<IfModule mod_rewrite.c>
    RewriteEngine On

    RewriteCond %{REQUEST_URI}:: ^(/.+)/(.*)::$
    RewriteRule ^(.*) - [E=BASE:%1]

    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

    RewriteCond %{ENV:REDIRECT_STATUS} ^$
    RewriteRule ^app_dev\.php(/(.*)|$) %{ENV:BASE}/ [R=301,L]

    RewriteCond %{REQUEST_FILENAME} -f
    RewriteRule .? - [L]

    RewriteRule .? %{ENV:BASE}/app_dev.php [L]
</IfModule>

好吧,我找到了解决我的问题的方法,也许不是最好的或最简单的方法,但是 Christian Flothmann here on this comment 推荐的。 Apache虚拟主机定义如下所示:

<VirtualHost *:80>
    ServerName server1.com
    ServerAlias server2.com
    ServerAlias server3.com

    DocumentRoot /var/www/html/web

    # if we're setting up production environment
    # DirectoryIndex app.php

    # if we're setting up development or qa environment
    # DirectoryIndex app_dev.php

    <Directory /var/www/html/web>
        AllowOverride None #important rule, do not allow any .htaccess
        Order allow,deny
        Allow from All

        RewriteEngine On
        RewriteCond %{REQUEST_URI}:: ^(/.+)/(.*)::$
        RewriteRule ^(.*) - [E=BASE:%1]

        RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

        RewriteCond %{ENV:REDIRECT_STATUS} ^$
        RewriteRule ^app\.php(/(.*)|$) %{ENV:BASE}/ [R=301,L]

        RewriteCond %{REQUEST_FILENAME} -f
        RewriteRule .? - [L]

        # if we're setting up production environment
        # RewriteRule .? %{ENV:BASE}/app.php [L]

        # if we're setting up development or qa environment
        # RewriteRule .? %{ENV:BASE}/app_dev.php [L]
    </Directory>

    ErrorLog /var/log/httpd/error.log

    LogFormat "%h %l %t \"%r\" %>s %b" common
    CustomLog /var/log/httpd/access.log common
</VirtualHost>

Note: It is important to read reviews, some rules must be applied depending on the environment that we will configure that means comments at begin of the line should be removed for properly functioning of Apache and application itself.