我正在使用 Yii 2
并且我想启用 pretty URL's
以便我可以访问以下内容:
www.example.com/controller/method
根据我的发现,您在 config/web.php
的 components 部分添加了以下代码:
'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):
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,
enableStrictParsing:该属性决定是否启用严格的请求解析。如果启用严格解析,传入请求的 URL 必须至少匹配其中一条规则才能被视为有效请求,否则将抛出 yii\web\NotFoundHttpException。如果禁用严格解析,则当没有规则匹配请求的 URL 时,URL 的路径信息部分将被视为请求的路由。
关于php - 尝试使用 Yii 2 启用漂亮的 URL 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27124544/