In many cases, it is very important for SEO to have a unique address for each page.
In many cases, it is very important for SEO to have a unique address for each page.
For example, we have the following rules:
'post/
'=>'post/read',
The following URLs are valid:
post/read/id/5 post/read?id=5 post/5
If we have a suffix, for example. Html, The following URL is valid,
post/read/id/5.html post/read.html?id=5 post/5.html
There are 6 URLs in total, which does not match SEO,
The solution is simple. you only need to write the code in your Controller:
public function beforeAction($action) { if (Yii::app()->request->url != CHtml::normalizeUrl(array_merge(array($this->route), $_GET))) $this->redirect(CHtml::normalizeUrl(array_merge(array($this->route), $_GET)), true, 301); return parent::beforeAction($action); }
This will redirect all URL addresses to a specified 'valid Address'. replacing 301 with 302 will make the browser (google robot) know the correct address.
You can write a base class for implementation:
Request-> url! = CHtml: normalizeUrl (array_merge (array ($ this-> route), $ _ GET) $ this-> redirect (CHtml :: normalizeUrl (array_merge (array ($ this-> route), $ _ GET), true, 301); return parent: beforeAction ($ action );}}
Tip | you should not use this rule in actionError and actionIndex. Therefore, if you want to use a unique URL for actions in siteController, you should not inherit the base class mentioned above, otherwise, you will use a special method to handle the exceptions generated by both of them.
This article translated from foreign language website, view the original, please click: http://www.yiiframework.com/wiki/219/how-to-ensure-unicity-to-url/