Add canonical links to your site [SEO]

Do something cool with CMS? Show us ...
This board is for 'Answers', and the discussion of answers... Not for questions.
Post Reply
Peciura

Add canonical links to your site [SEO]

Post 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}
dmaireroa
Forum Members
Forum Members
Posts: 110
Joined: Wed Jul 15, 2009 12:35 am

Re: Add canonical links to your site [SEO]

Post 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.
Post Reply

Return to “Tips and Tricks”