Yii2 从 url 中删除 index.php

Yii2 remove index.php from url

当我在 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 文件中显示的别名。

此 url 工作正常 http://13.21.16.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>

您应该像这样设置 Url 管理器组件:

'urlManager' => [
    'enablePrettyUrl' => true,
    'showScriptName' => false, // Only considered when enablePrettyUrl is set to true
],

官方文档:

我认为是您的 Apache 配置有误...您告诉它只重写我正在阅读的单个字符的请求?

我认为应该是:

RewriteRule ^/(.*) index.php/ [L]

但我可能是错的,不是 apache 专家

RewriteEngine on后添加RewriteBase /decat/并重启apache。

您应该将 .htaccess 更改为

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

和url经理规则为

 '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>',
    ),
    ],

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

yii2 从 url

中删除网页

1) 在

处使用以下内容编辑您的 config/web.php 文件
<?php
use \yii\web\Request;

$baseUrl = str_replace('/web', '', (new Request)->getBaseUrl());

return [
    ...
    'components' => [
            'request' => [
                'baseUrl' => $baseUrl,
     ],
      ...
    ]
]
?>

2) 添加以下 htaccess 到 root/web

RewriteEngine On RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule . index.php

3) 将以下 htaccess 添加到安装应用程序的根文件夹中

# prevent directory listings
Options -Indexes
IndexIgnore */*

# follow symbolic links
Options FollowSymlinks
RewriteEngine on
RewriteRule ^(.+)?$ web/