urlManager 404 not found because remove "/" at the start of the route

Hi alle, i have this urlManager configuration:

        'urlManager' => [

            'enablePrettyUrl' => true,

            'showScriptName' => false,

            'normalizer' => [

                'class' => 'yii\web\UrlNormalizer',

                'collapseSlashes' => true,

                'normalizeTrailingSlash' => true,

                'action' => null,


            'rules' => [


                    'pattern'       => 'ug/<namespace:[a-z0-9]+(?:-[a-z0-9]+)*>/user',

                    'route'         => '/user/login',



                    'pattern'   => '/ug/<namespace:[a-z0-9]+(?:-[a-z0-9]+)*>/',

                    'route'     => 'user-gallery',




with the request: "example.com/xxx" it works fine and redirect "example.com/user-gallery"

with the request: “example.com/xxx/user” it doesn’t work with the exception “404 not found” and the message:

yii\base\InvalidRouteException Object ( [message:protected] => Unable to resolve the request "xxx/user/login"

with the request: "example.com/user/login" i have the right login page.

I think my problem is that the urlManager remove the "/" from the start of the route, beacuse the request "/xxx/user/login" works fine.

I try change the route in "user/login", "/user/login", "//user/login" but i see in the source code of yii2/framework/web/UrlRule.php

$this->route = trim($this->route, '/');

I see also that i don’t have a controller UserController.php because it is a module.

If i change the route into "site/index" works fine.

So i don’t know how can i do.

Any suggestion?


I don’t understand what these rules are expected to do. What do ‘ug’ and ‘namespace’ mean in the url patterns?

I also don’t understand why it works. “example.com/xxx” doesn’t match either ‘ug/<namespace:[a-z0-9]+(?:-[a-z0-9]+)>/user’ or '/ug/<namespace:[a-z0-9]+(?:-[a-z0-9]+)>/’ and should be treated as “404 not found”.

Or, do you have other rules that you didn’t show us?

Yes sorry, i miss "/ug/" in the path for the right request.

"example.com/ug/xxx" points well to "example.com/user-gallery/xxx" with namespace=xxx [match my second rule]

Now i want that “example.com/ug/xxx/user” point to “example.com/user/login” [first rule]. This works only if i have a controller called “UserController”. But i don’t have it. I only have a module “User”.

In other words i think Yii2 UrlManager doesn’t work (or i don’t know how to do) if you create a rule with a route that point not to a controller. In my case “example.com/user/login” works because i have a module “User” and not a “UserController”. But if i write ‘route’ => ‘/user/login’ the rule match but i have 404 not found, because the final request is “user/login” and not “/user/login”. This last request works.

Thank’s for reply.

I see.

Then you could change the ‘route’ to point to the controller/action in ‘user’ module.


                    'pattern'       => 'ug/<namespace:[a-z0-9]+(?:-[a-z0-9]+)*>/user',

                    'route'         => 'user/some-controller/login',


Note that route can contain an ID of a module.

Guide > Routing and URL > Routing