Question

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"?

Was it helpful?

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 */));
}

OTHER TIPS

THis is my best solution:

class Controller extends CController {
        public function beforeAction($a) {
            if(strlen(Yii::app()->request->url)>1)
                if(!$strpos = strpos(Yii::app()->request->url,'.html')) {
                    //html not found... need to correect
                    if(!($strpos = strpos(Yii::app()->request->url,'?'))) {
                        $this->redirect(Yii::app()->request->url.".html", true, 301);
                    } else {
                        $newUrl = substr(Yii::app()->request->url,0,$strpos).".html".substr(Yii::app()->request->url,$strpos);
                        $this->redirect($newUrl, true, 301);
                    }                          
                };


            return true;
        }
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top