How does Yii ensure the uniqueness of a URL?
For example, we have the following rules:
1 |
'Post/ '=> 'Post/read ', |
The following URLs are valid:
123 |
Post/read/id/5 post/read? Id = 5 post/5 |
If we have a suffix, for example. Html, The following URL is valid,
123 |
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:
12345678 |
PublicfunctionbeforeAction ($ 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); returnparent: 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:
1234567891011121314151617 |
Request-> url! = CHtml: normalizeUrl (array_merge (array ($ this-> route), $ _ GET) $ this-> redirect (CHtml :: normalizeUrl (array_merge (array ($ this-> route), $ _ GET), true, 301); returnparent: 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.