Canonical urls

I am developing a website where one item can be assigned to multiple categories. I was advised that the proper implementation should consist of one main category and the rest should lead to canonical urls (supposedly google likes it that way). Has anybody implemented canonical urls with Yii and what design did you follow so they can be managed from backend. Any insiders would be highly appreciated.

Thanks in advance,

bettor

Because I bumped into this topic via the google search, here is my simple solution:

In components/Controller (base controller):




protected $_canonicalUrl;


/**

 * Default canonical url generator, will remove all get params beside 'id' and generates an absolute url.

 * If the canonical url was already set in a child controller, it will be taken instead.

 */

public function getCanonicalUrl() {

    if ($this->_canonicalUrl === null) {

        $params = array();

        if (isset($_GET['id'])) {

            //just keep the id, because it identifies our model pages

            $params = array('id' => $_GET['id']);

        }

        $this->_canonicalUrl = Yii::app()->createAbsoluteUrl($this->route, $params);

    }

    return $this->_canonicalUrl;

}



In views/layouts/main:




<head>

  <link rel="canonical" href="<?php echo $this->canonicalUrl?>">

</head>



Any comments to improve are appreciated.

I also found a work around it but thought there might be a native Yii way of doing it that I might have missed

Then open a ticket for it at Github - or better: implement it in a fork and issue a pull request.

This might be hard, because for canoncial urls Yii would have to know which part of the URI is "canonical" and which not. And because URIs are a matter of your personal design (and how many params you pass around) I think this should not be done by Yii.