Symfony .htaccess DirectoryIndex不工作

时间:2015-09-26 10:35:16

标签: .htaccess symfony production

我正在将symfony应用程序部署到生产环境中,我已将symfony应用程序放在根文件夹中 / var / www / html并将apache2根目录设置为/ var / www / html / web但.htaccess文件中的DirectoryIndex未重定向到文件app.php

在url xxx.xxx.xx.xx / i上获取/ var / www / html / web文件夹中所有文件的列表,如果我执行xxx.xxx.xx.xx / app.php应用程序负载。

我不明白如何让.htaccess中的DirectoryIndex工作。

我一直在浏览以下堆栈问题类型,但它们对我不起作用:

另一篇文章建议将apache2根目录设置为/ var / html而不是/ var / html / web,但这也不起作用。因为url xxxx.xxx.xx.xx / deplays目录/ var / html的内容

我最接近的解决方案是将文件app.php重命名为index.php,它将根网址xxx.xxx.xx.xx /加载,但随后是所有'sub'网址(如xxxx) .xxx.xx.xx / offer / 9 / Alternance_Apprentissage_graphiste_chez_Eggs)因404 Not Found错误而失败。 这是由于.htaccess文件未正确重定向网址,因为网址xxxx.xxx.xx.xx / app.php / offer / 9 / Alternance_Apprentissage_graphiste_chez_Eggs工作正常。

我的.htaccess文件看起来像这样(它是默认生成的文件):

DirectoryIndex app.php

<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>

<IfModule !mod_rewrite.c>
    <IfModule mod_alias.c>
        RedirectMatch 302 ^/$ /app.php/
    </IfModule>
</IfModule>

任何帮助表示赞赏。这很关键,因为我无法让我的应用程序继续生产。

更新

@shevron是对的。从他的回答“你的Apache设置可能根本没有解析.htaccess文件。”和“在该目录上检查主要的Apache配置是否为AllowOverride。尝试将其设置为AllowOverride All并重启Apache - 看看是否有效。”

我做了以下事情:

我将Apache配置设置如下:

 <VirtualHost *:80>

     ServerAdmin webmaster@localhost
      DocumentRoot /var/www/html/web
      <Directory "/var/www/html/web">
        AllowOverride All
        Allow from All
      </Directory>

      ErrorLog ${APACHE_LOG_DIR}/error.log
      CustomLog ${APACHE_LOG_DIR}/access.log combined

   </VirtualHost>

这不起作用。 @shevron我没有写得那么好吗?

但是我进一步将.htaccess文件中的所有配置都放到了主Apache配置中,这使重定向工作:

  <VirtualHost *:80>
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html/web
    <Directory "/var/www/html/web">
          AllowOverride All
          Allow from All
    </Directory>

    DirectoryIndex app.php

    <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>

    <IfModule !mod_rewrite.c>
        <IfModule mod_alias.c>
            RedirectMatch 302 ^/$ /app.php/
        </IfModule>
    </IfModule>

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

  </VirtualHost>

所以现在重定向工作正常。

1 个答案:

答案 0 :(得分:2)

您的Apache设置可能根本不解析.htaccess文件(事实上,出于性能和安全原因,可能建议单租户生产服务器使用。)

检查该目录上的AllowOverride主Apache配置。尝试将其设置为AllowOverride All并重新启动Apache - 看看是否有效。

如果有效,我建议将.htaccess文件的内容移动到主Apache配置文件中的<Directory><VirtualHost>部分(或者更好地在其包含的单独文件中)和设置AllowOverride None

我发现了这个:http://symfony-check.org/permalink/optimize-apache-avoid-htaccess这是Symfony特有的一些细节,虽然我不知道它是否是最新的,因为我不是Symfony用户。