2

我有这个 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>

如果我http://server1.qa.com没有打电话,app_dev.php我会以这个错误结束:

由于可能的配置错误,请求超出了 10 个内部重定向的限制。如有必要,使用“LimitInternalRecursion”增加限制。使用“LogLevel debug”获取回溯。

如果我打电话,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}::$1 ^(/.+)/(.*)::\2$
    RewriteRule ^(.*) - [E=BASE:%1]

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

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

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

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

访问日志

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
4

3 回答 3

0

好吧,我找到了解决我的问题的方法,也许不是最好的解决方案,也不是最简单的解决方案,但Christian Flothmann在此评论中推荐。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}::$1 ^(/.+)/(.*)::\2$
        RewriteRule ^(.*) - [E=BASE:%1]

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

        RewriteCond %{ENV:REDIRECT_STATUS} ^$
        RewriteRule ^app\.php(/(.*)|$) %{ENV:BASE}/$2 [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>

注意:阅读评论很重要,必须根据我们将配置的环境应用一些规则,这意味着应该删除行首的评论,以便 Apache 和应用程序本身正常运行。

于 2015-09-06T18:52:23.063 回答
0

你有你的虚拟主机设置,所以这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}::$1 ^(/.+)/(.*)::\2$
    RewriteRule ^(.*) - [E=BASE:%1]

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

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

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

    RewriteRule .? %{ENV:BASE}/app_dev.php [L]
</IfModule>
于 2015-09-04T16:26:33.030 回答
0

好的,我会再试一次。

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

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

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

于 2015-09-04T16:00:38.677 回答