尝试使用 Yii 2 启用漂亮的 URL 时出错


Getting error when attempting to enable pretty URL's with Yii 2

我正在使用Yii 2,我想启用pretty URL's以便我可以访问以下内容:

www.example.com/controller/method

根据我发现的内容,您在config/web.php组件部分添加了以下代码:

'urlManager' => [
    'enablePrettyUrl' => true,
    'showScriptName' => false,
    'enableStrictParsing' => true,
    'rules' => [
    ],
],

但是,当我加载索引页时,我现在收到此错误:

exception 'yii'base'InvalidRouteException' with message 'Unable to resolve the request: site/error' in *************'vendor'yiisoft'yii2'base'Controller.php:122

还有前面的例外:

exception 'yii'web'NotFoundHttpException' with message 'Page not found.' in ***************************'vendor'yiisoft'yii2'web'Request.php:187

这是我.htaccess文件:

# 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

是否在规则部分遗漏了某些内容,或者我在这里做错了什么?

尝试删除 'enableStrictParsing' => true,

启用严格解析

:此属性确定是否启用严格请求解析。如果启用了严格解析,则传入请求的 URL 必须至少与其中一个规则匹配才能被视为有效请求,否则将抛出 yii''web''NotFoundHttpException。如果禁用了严格解析,则当没有任何规则与请求的 URL 匹配时,URL 的路径信息部分将被视为请求的路由。