Yii2从url中删除index.php

Mic*_*air 7 php yii yii2

当我在Yii中设置选项以从URL中删除index.php时,我收到404错误并在错误日志中出现此错误File does not exist: /var/live/var.在我的浏览器中,我收到此错误,The requested URL /var/decat/frontend/web/index.php was not found on this server.但文件正好在该位置.可以解释的是,我的文档根目录是/var/live和decat是别名,如conf文件中所示.

这个网址工作正常http://130.211.165.180/decat/index.php/site/login但是当我删除index.php时,我收到错误.我按照所有说明在conf文件中进行设置.我甚至尝试过.htaccess文件.这是我的conf文件中的信息.

Alias /decat /var/decat/frontend/web

<Directory "/var/decat/frontend/web">
        # use mod_rewrite for pretty URL support
        RewriteEngine on
        # If a directory or a file exists, use the request directly
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        # Otherwise forward the request to index.php
        RewriteRule . index.php 
        Options -Indexes FollowSymLinks 
        AllowOverride All
        Order allow,deny 
        Allow from all 
</Directory>
Run Code Online (Sandbox Code Playgroud)

Via*_*lav 7

添加RewriteBase /decat/RewriteEngine on重启apache.


aro*_*hev 6

您应该像这样设置 Url Manager 组件:

'urlManager' => [
    'enablePrettyUrl' => true,
    'showScriptName' => false, // Only considered when enablePrettyUrl is set to true
],
Run Code Online (Sandbox Code Playgroud)

官方文档:


FSS*_*ikh 5

您应该将 .htaccess 更改为

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\?*$ index.php?r=$1 [L,QSA]
Run Code Online (Sandbox Code Playgroud)

和 urlManager 规则为

 'urlManager' => [
    'class' => 'yii\web\UrlManager',
    // Disable index.php
    'showScriptName' => false,
    // Disable r= routes
    'enablePrettyUrl' => true,
    'rules' => array(
            '<controller:\w+>/<id:\d+>' => '<controller>/view',
            '<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
            '<controller:\w+>/<action:\w+>' => '<controller>/<action>',
    ),
    ],
Run Code Online (Sandbox Code Playgroud)

我在我的应用程序中使用它。它有效。我的网址鞋为www.example.com/user/view?id=1