Canonical URL
- Details
- Created on Wednesday, 22 February 2012 02:11
So on my holiday, we had a couple of days of rain, complimented by the hurricane IRENE. I used my time, knowing how precious it is. And made a dynamically changing Canonical URL, for my Joomla Templates. This one was fairly easy, but it is quite handy.
All SEO people add the "canonical" URL to landingpages and other important pages, to avoid duplicate content. Typically if the damage is already done, this will inform Google, what URL you wan't all your "rank/link-juice to be passed to. But you might as well include this code snippet in your header anyway, then you don't need to think about that anymore!
Although to get the right URL with http://www. you should also add, a permanent redirect in your .htaccess file! (You can find this code further down).
<!--Dynamic Canonical-->
<?php
$u =& JURI::getInstance();
$url = $u->toString();
?>
<link rel='canonical' href='<?php echo $url ?>' />
Attention: This code will not work, outside of joomla, since I used buid in JURI, to get the URL! The code should be placed inside <head></head>
RewriteCond %{HTTP_HOST} !^www.danieldue.dk$ [NC]
RewriteRule ^(.*)$ http://www.danieldue.dk/$1 [R=301,L]
Attention: The above code, should be modified to match your domain, and be placed in the .htaccess file.
