Brett
Brett

Reputation: 20099

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

I'm using Yii 2 and I want to enable pretty URL's so that I can access stuff like:

www.example.com/controller/method

From what I found you add the below code in the components section of config/web.php:

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

    ],
],

However when I load the index page I now get this error:

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

There is also this previous exception:

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

This is my .htaccess file:

# 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

Am I missing something in the rules section or what am I doing wrong here?

Upvotes: 3

Views: 3503

Answers (1)

Mihai P.
Mihai P.

Reputation: 9367

Try removing 'enableStrictParsing' => true,

enableStrictParsing: this property determines whether to enable strict request parsing. If strict parsing is enabled, the incoming requested URL must match at least one of the rules in order to be treated as a valid request, or a yii\web\NotFoundHttpException will be thrown. If strict parsing is disabled, when none of the rules matches the requested URL, the path info part of the URL will be treated as the requested route.

Upvotes: 4

Related Questions