Page 1 of 1

Add canonical links to your site [SEO]

Posted: Wed Oct 06, 2010 3:09 pm
by Peciura
Idea is to tell googol link to a page with only required url params. More info http://googlewebmastercentral.blogspot. ... nical.html

I created UDT that strips off extra params in url. Than it can be used as canonical url. This UDT is created for Products detail view but can easily be adopted to any other module. It uses "ugly" url and $config['process_whole_template'] = false;.
/**
* Get product details canonical url
*
* @params string $params['url_param_list'] Comma separated list of parameters to be kept in new url. DefaultĀ  'mact,cntnt01productid,hl'
* @params string $params['url'] URL to be modified. Default $config['root_url'].$_SERVER['REQUEST_URI'];
* @params string $params['assign'] Assign value to.
*
*/

//prepare parameter
if(empty($params['url_param_list'])){
$url_param_list = 'mact,cntnt01productid,hl';
}
else{
$url_param_list = trim($params['url_param_list']);
}
if(empty($params['url'])){
$config = cmsms()->GetConfig(); // cms_utils :: get_config();
$url = $config['root_url'].$_SERVER['REQUEST_URI'];
}
else{
$url_param_list = trim($params['url']);
}
$return = array();

//modify url query
$query_string = parse_url($url, PHP_URL_QUERY);
$url_params_to_save = explode(',', $url_param_list);
parse_str($query_string, $data);
foreach($data as $key => $url_param){
if(!in_array($key, $url_params_to_save)){
unset($data[$key]);
}
}
$url_query = http_build_query($data, '', '&');

//recreate url
$return = str_replace($query_string, $url_query, $url);

//assign to smarty if needed
if(!empty($params['assign'])){
$smarty = cmsms()->GetSmarty(); // cms_utils :: get_smarty();
$assign = trim($params['assign']);
$smarty->assign($assign, $return);
}
else{
return($return);
}
Call it in Products detail template
{get_canonical_url assign='canonical_url' url_param_list='mact,cntnt01productid,hl'}
Add canonical link to meta data ("Site Admin Ā» Global Settings > Global Settings > Global Metadata:")
{if !empty($canonical_url)}

{/if}

Re: Add canonical links to your site [SEO]

Posted: Wed Dec 15, 2010 2:47 pm
by dmaireroa
I noticed no one hasn't replied yet. I was reading the link you sent earlier, and didn't know my site was using a 'www' and 'http://'.

Thanks, will look deeper into it.