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!

View Replies:
PatrickHeck replied on at Permalink Reply
PatrickHeck
Hi Northblue,

try this solution taken from http://concrete5packages.com/concrete5_blog/canoncial-urls/...

<? $cPath = $c->getCollectionPath();
$canonicalURL = BASE_URL;
$canonicalURL.= $cPath;
$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 ?>" />


Best Patrick
northblue replied on at Permalink Reply
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?
firepixel replied on at Permalink Reply
firepixel
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 ?>" />
PatrickHeck replied on at Permalink Reply
PatrickHeck
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.
<?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 ?>" />
madesimplemedia replied on at Permalink Reply
madesimplemedia
Just implemented this, works great thank you very much!
dantheman replied on at Permalink Reply
dantheman
This is really helpful - thank you.