Search code examples
phpyiiyii-url-manager

Yii auto correct current uri with redirect


THere's no secret, that Yii thinks those are equal addresses:

And config:

...
'urlFormat'=>'path',
'urlSuffix'=>'.html',
'showScriptName'=>false,
...

What is the best way to make Yii correct current URI if it has no ".html"?


Solution

  • Well, you should simply try this :

    'urlFormat'=>'path',
    'showScriptName'=>false,
    'rules'=>array(
        '<controller:\w+>/<action:\w+>\.html'=>'<controller>/<action>',
        '<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
    ),
    

    And to handle redirect, add this in your action (or in beforeAction or a filter) :

    if (substr(Yii::app()->request->url, -5)!=='.html')
    {
        $this->redirect(array($this->action->id, /* add your action params here */));
    }