Adding Canonical URL from code
Permalink
Hi guys,
I am trying to add a Canonical URL from my block or template's code.
I think it should be something like:
$c->rescanPagePaths('/newtest2');
But it is not working for me.
Can anyone give me a hand on that?
Any response will be appreciated.
A piece of sample code or a link will be great.
Thanks!
I am trying to add a Canonical URL from my block or template's code.
I think it should be something like:
$c->rescanPagePaths('/newtest2');
But it is not working for me.
Can anyone give me a hand on that?
Any response will be appreciated.
A piece of sample code or a link will be great.
Thanks!
Hi Patrick,
Thanks a lot for the response.
I will give a try for this solution.
Also I would like to ask if anyone played around with rescanPagePath function?
Thanks a lot for the response.
I will give a try for this solution.
Also I would like to ask if anyone played around with rescanPagePath function?
This is a useful piece of code, but I always add an extra line since Google Webmaster Tools still tells me I have two versions of the page depending on weather it ends in a forward slash or not (which is just ridiculous if you ask me). C5 accepts it either way, but it's a nightmare to make sure all external links are the same.
<?PHP $cPath = $c->getCollectionPath(); $canonicalURL = BASE_URL; $canonicalURL.= $cPath; if(!(substr($cPath, strlen($cPath)-1, 1)=="/")) $canonicalURL .= "/"; $pageIndentifierVars = array('keywords','fID','tag','productID'); $canonicalVars = array(); foreach($pageIndentifierVars as $var) if($_REQUEST[$var]) $canonicalVars[]= $var.'='.$_REQUEST[$var]; if( count($canonicalVars) ) $canonicalURL.= '?' . join(',',$canonicalVars); ?> <link rel="canonical" href="<?= $canonicalURL ?>" />
That makes sense to me.
Actually I realized that two other lines should be includes to honour cases where C5 is accesses via a subdirectory or where index.php has to be included.
Actually I realized that two other lines should be includes to honour cases where C5 is accesses via a subdirectory or where index.php has to be included.
<?PHP $cPath = $c->getCollectionPath(); $canonicalURL = BASE_URL; $canonicalURL.= DIR_REL; $canonicalURL.= URL_REWRITING?"":"/index.php"; $canonicalURL.= $cPath; if(!(substr($cPath, strlen($cPath)-1, 1)=="/")) $canonicalURL .= "/"; $pageIndentifierVars = array('keywords','fID','tag','productID'); $canonicalVars = array(); foreach($pageIndentifierVars as $var) if($_REQUEST[$var]) $canonicalVars[]= $var.'='.$_REQUEST[$var]; if( count($canonicalVars) ) $canonicalURL.= '?' . join(',',$canonicalVars); ?> <link rel="canonical" href="<?= $canonicalURL ?>" />
Just implemented this, works great thank you very much!
This is really helpful - thank you.
try this solution taken from http://concrete5packages.com/concrete5_blog/canoncial-urls/...
Best Patrick