Yii-how to ensure URL uniqueness

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


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/

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.