PrettyURL don't work on custom controllers

currently I am trying to set up pretty url’s in my project (Yii2) but it doesn’t work like I thought.
In my config/web.php I added the ‘urlManager’

        'urlManager' => [     
        'showScriptName' => false,	// Disable index.php
        'enablePrettyUrl' => true,	// Disable r= routes
        'enableStrictParsing' => true,
        'rules' => array(
            '' => 'site/index',
            '<controller:\w+>/<id:\d+>' => '<controller>/view',
        	'<controller:\w+>/<action:\w+>/<id:\d+>' => '<controller>/<action>',
        	'<controller:\w+>/<action:\w+>' => '<controller>/<action>',

I created a .htaccess file

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule . index.php

and placed it into the web folder. Xampp should be configured correctly.
I have 2 controllers. First on is the standard controller -> SiteController. The second is a controller created by myself TemplateController.

If I type localhost/site/login or localhost/site/contact everything is working well. The defined action is getting called. But if I try to open localhost/template/test I get an error that the page couldn’t be found.

Now I added a new rule to the urlManager

'template/test' => 'template/test',

After adding this, it works. But why the action doesn’t get called without the rule? I thought the last standard rule

'<controller:\w+>/<action:\w+>' => '<controller>/<action>',

will be executet in this case and I don’t need to add an extra rule. Why does it work with the site controller? Are there any more settings I need to make?

Maybe it’s inside the controller? The SiteController has the methods behaviors and actions. My TemplateController only extends from Controller but don’t implement both methods. Just the action test.

class TemplateController extends Controller
    public function actionTest(){
        echo "test";

Maybe someone of you guys can help me. Thanks alot.

Your last rule should actually cover it. I’ve tried with a single rule of

'<controller:\w+>/<action:\w+>' => '<controller>/<action>',

and it works.

Thanks for your comment @samdark
Do you have any other ideas why it doesn’t work?

After deleting the custom rule

'template/test' => 'template/test',

I get this error page:

In my apache httpd.conf file the rewrite_module is loaded.

No idea. I’m using nginx though, not Apache.

Works on Win10/Xampp. (Fresh install XAMPP Version: 7.3.2/Yii 2.0.16, first time I try this environment.)
Works with full path, works with changed DocumentRoot (…Yii2016/web)

Okay. I deleted this line in my urlManager

'enableStrictParsing' => true,

after this it works. I have no idea why it didn’t work before because the documentation ($enableStrictParsing-detail) say

Whether to enable strict 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 does this mean I need for each URL a custom rule and the template-rules don’t work for it?

But now I can progress my work. Thanks to both of you!

I used the UrlManager config in your first post (i.e. no custom rule, enableStrictParsing => true).

If you enable the debugger, under “Router” you can see a message like this (as well as the preceeding steps)
Request parsed with URL rule: <controller:\w+>/<action:\w+>

Didn’t knew this debug option. Thanks for the hint. The route /template/test is working now. It uses the last rule


When I try to access one other action which is located in the TemplateKeyController


I get displayed that all 8 routes getting tested but there is no match. Is there anything special I need to note when there is a hyphen in the url?

My Controller is called TemplateKeyController and the action inside actionGenerateKey.

Yes. You’ve defined patterns without hyphens as \w+ that is a regular expression for one or more letters.