Yii - Rewrite Url Via Urlmanager

I’m trying to rewrite an url created via createUrl method:


Yii::app()->createUrl('user/user/view',array('id'=>$id));

In urlManager, I set a rule that almost like the instruction:


'urlManager'=>array(

    'urlFormat'=>'path',

    'showScriptName' =>false,

    'rules'=>array(

       // core

       '<controller:\w+>/<id:\d+>'=>'<controller>/view',                                

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

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

       //this url

       'user/<id:\w+>'=>array('user/user/view', 'urlSuffix'=>'.html'),

     ),

),

It worked without any problem.

Now I want to remove the .html extension with the rule below instead:


'user/<id:\w+>'=>'user/user/view',

And it returns error

. It seems that the regex pattern


<id:\w+>

used to rewrite url, is no longer correct. How could I fix it?

Put this rule on the top of the rules definition. Also, if user id is numerical, you should use <id:\d+> instead of <id:\w+>

Thank you, rob006. You pointed out the exact problem. Did that mean some of my URLs are duplicated?

Url manager gets first matching rule, when resolves urls. Url like example.com/user/1 match rule ‘<controller:\w+>/<id:\d+>’=>’<controller>/view’ so this is used. You just need put more specific rules on the top.