0

这已经不是什么秘密了,Yii 认为这些是相等的地址:

和配置:

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

如果没有“.html”,使 Yii 正确的当前 URI 的最佳方法是什么?

4

2 回答 2

1

好吧,你应该简单地试试这个:

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

要处理重定向,请将其添加到您的操作中(或在 beforeAction 或过滤器中):

if (substr(Yii::app()->request->url, -5)!=='.html')
{
    $this->redirect(array($this->action->id, /* add your action params here */));
}
于 2013-10-09T15:08:21.073 回答
0

这是我最好的解决方案:

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;
        }
    }
于 2014-01-12T05:43:22.067 回答