首页 文章

Symfony .htaccess DirectoryIndex不起作用

提问于
浏览
1

我正在将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,它将根url 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 回答

  • 2

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

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

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

    我找到了这个:http://symfony-check.org/permalink/optimize-apache-avoid-htaccess这是Symfony特有的一些细节,虽然我不是't know if its up-to-date as I' m不是Symfony用户 .

相关问题