How does Yii ensure the uniqueness of a URL?

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.