尝试从网址中删除index.php

时间:2019-05-03 15:56:05

标签: .htaccess routes symfony4 production-environment

**我正在使用托管在ubuntu-server.16.04上的apache安全的Web服务器上

我正在发送关于symfony的第一个项目,从现在开始我做得很好。我正在尝试重写url,因此当我要进入项目仪表板时,URL将是“ ../dashboard/”而不是(实际上是这样的)“ ../ index.php / dashboard /”

但是当我尝试使用该URL时,服务器将我重定向到symfony应用之外,并向我抛出一个基本的apache 404错误,而不是symfony处理的错误。

我认为可以在.htaccess文件中进行编辑,但我无法找到如何使他执行所有URL作为symfony路由的方法,这是我的问题。

尝试在Google上找到多个代码

htaccess

DirectoryIndex index.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 ^index\.php(?:/(.*)|$) %{ENV:BASE}/$1 [R=301,L]
    RewriteCond %{REQUEST_FILENAME} -f
    RewriteRule ^ - [L]
    RewriteRule ^ %{ENV:BASE}/index.php [L]
</IfModule>

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

1 个答案:

答案 0 :(得分:1)

您需要启用apache重写。 请按照以下步骤操作: 在终端中,使用以下命令:

$sudo a2enmod rewrite

更改apache conf文件中的AllowOverride:

$sudo nano /etc/apache2/apache2.conf

在此块中将AllowOverride从“无”更改为“全部”

<Directory /var/www/>
    Options Indexes FollowSymLinks
    AllowOverride All
    Require all granted
</Directory>

最后,重新启动apache2

$sudo service apache2 restart