如何从 Symfony4 路由器中删除 index.php?

How to remove index.php from Symfony4 router?

我一直在关注有关如何配置 Web 服务器的 Symfony 4 文档。

https://symfony.com/doc/current/setup/web_server_configuration.html

我的 apache 2.4 配置中的 .conf 文件与他们的文档中描述的完全一样。我在这里复制了一部分:

<Directory /var/www/project/public>
    AllowOverride None
    Require all granted
    Allow from All

    <IfModule mod_rewrite.c>
        Options -MultiViews
        RewriteEngine On
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteRule ^(.*)$ index.php [QSA,L]
    </IfModule>
</Directory>

转到我的平台,一切正常,但只要我转到任何页面,它就会显示 404 错误。如果我在 url 中添加 index.php 它工作正常。你能帮我弄清楚这是怎么回事吗?

例如: 当我在 http://myplatform.com/

我可以在此页面上保存电子邮件地址。如果我去 http://myplatform.com/index.php/saveEmail it works, but from my page, my links redirects me to http://myplatform.com/saveEmail 但它不起作用。

如果我去 http://myplatform.com/index.php 那么一切正常。

如何从 url 中删除 index.php?

谢谢!

此 apache 配置不包括为每个请求重定向到 index.php。您有 2 个选择:

• 在同一页面上,有一个配置在这句话之后包含此重定向:"Use the following optimized configuration to disable .htaccess support and increase web server performance:"

• 如果你想使用.htaccess 来处理这个重定向,那么你可以简单地执行composer require apache-pack。此命令将在您的 public 目录中安装 .htaccess。

如果你这样改变它也许可以工作,

AllowOverride All
Order Allow,Deny

我发现了问题。 <Directory /var/www/project/public> 指向错误的目录。

我不知道 Symfony。但它应该适用于所有 apache2 和 php 项目。


首先你需要做两件事:

  1. 您需要检查 php 重写模块是否启用。你可以用 phpinfo() 来完成。将其写入文件 info.php 并在浏览器中打开:http://localhost/info.php

    现在搜索 mod_rewrite。如果您在此页面中看到 mod_rewrite,则表示 mod_rewrite 已启用。

  2. 如果未启用mod_rewrite,请启用它。我使用以下命令在 ubuntu 18.04 中启用了 apache:$ sudo a2enmod rewrite。然后重启apache2$ systemctl restart apache2

如果启用mod_rewrite,则其他配置将起作用。


  1. 然后在.conf文件中添加AllowOverride All

    <Directory /var/www/project/public>
        AllowOverride All
    </Directory>
    

  1. 然后在项目根目录下添加.htaccess文件。

    <IfModule mod_rewrite.c>
        RewriteEngine On
    
        # Send would-be 404 requests to Craft
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule (.+) index.php?p= [QSA,L]
    </IfModule>
    

注意:尝试在 /var/www/project/.htaccess/var/www/project/public/.htaccess 中添加 .htaccess 文件。不确定什么会起作用。