我有这个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
,我将以以下错误结束:
由于可能的原因,请求超过了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>
访问.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
您有vhost设置,因此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>
好的,我会再试一次。
我认为拥有两个.htaccess文件会更容易,每个环境一个:
- 从虚拟主机文件中删除DirectoryIndex
- 将.htaccess复制到.htaccess_dev
- 将.htaccess_dev中的所有"app.php"替换为"app_dev.php">
- 在您的虚拟主机中添加"AccessFileName.htaccess_dev"并重新启动您的apache
现在,你的.htaccess_dev将被使用,它不应该触及你的app.php文件。
好吧,我已经找到了一个解决我的问题的方法,也许不是最好的,也不是简单的,而是由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和应用程序本身正常运行。