Assign the new SEO rel="canonical" link tag in HEAD of page

Do something cool with CMS? Show us ...
This board is for 'Answers', and the discussion of answers... Not for questions.
JohnnyB
Dev Team Member
Dev Team Member
Posts: 731
Joined: Tue Nov 21, 2006 5:05 pm

Assign the new SEO rel="canonical" link tag in HEAD of page

Post by JohnnyB »

Search engines have agreed to adopt the rel="canonical" link to help prevent duplicate content in search engines.  This is especially important for CMSMS users because a page can be viewed using a variety of URLs.  For example,

http://domain.com/parent-alias/child-alias is accessible using:
http://domain.com/foo/child-alias
http://domain.com/child-alias

so, search engines need to know which is the 'real' page.  The tag will tell Google, Yahoo, and MSN Live that http://domain.com/parent-alias/child-alias is the real page regardless of how it is accessed.

The link should be placed in the .  Instead of setting each one individually using the metadata content of the page editor options tab, use this UDT.  (I grabbed code from a calguy1000 UDT at http://forum.cmsmadesimple.org/index.ph ... 440.0.html)

Create a User Defined Tag called 'canonical' using: EDITED 2-14-09

Code: Select all

global $gCms;
if ($gCms->variables['page_name'] !='') {
	$manager =& $gCms->GetHierarchyManager();
	$node =& $manager->sureGetNodeByAlias($gCms->variables['page_id']);
	
	$content =& $node->GetContent();
	if (isset($content) && is_object($content))
	{
		if ($content->GetURL() != '')
		{
		echo '<link rel="canonical" href="'.$content->GetURL().'" />';
		}
	}
}
It will produce a proper canonical link for you.  For more information about this soon to be very popular tag, read http://www.seomoz.org/blog/canonical-ur ... e-sitemaps and http://googlewebmastercentral.blogspot. ... nical.html


At some point I'll try to add a plugin to the forge. 

Question:

Does any one know if there are any implications when using SSL ?  Should the tag test for https somehow?

Code: Select all

 $pageURL = 'http';
 if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
 $pageURL .= "://";
 if ($_SERVER["SERVER_PORT"] != "80") {
  $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
 } else {
$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
 }
 return $pageURL;
Last edited by JohnnyB on Sun Feb 15, 2009 7:57 am, edited 1 time in total.
"The art of life lies in a constant readjustment to our surroundings." -Okakura Kakuzo

--
LinkedIn profile
--
I only speak/write in English so I may not translate well on International posts.
--
Jean le Chauve

Re: Assign the new SEO rel="canonical" link tag in HEAD of page

Post by Jean le Chauve »

Great work, thanks.

But when i use the Viebig's trick for the news titles (http://forum.cmsmadesimple.org/index.ph ... 475.0.html), i loose the indexation of the news.
This could be usefull in conjonction with your tag and the Viebig's trick :

Code: Select all

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<__html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr" lang="fr_FR">
<head>
{if $page_name=='news'}
{content assign="capturedcontent"}{/if}
<title>{if isset($pagetitle)}{$pagetitle} - {sitename}</title>{else}{title} - {sitename}</title>
{canonical}{/if}
{process_pagedata}
{metadata}
Take a look here for the details of $page_name : http://forum.cmsmadesimple.org/index.ph ... #msg145475
Bully
New Member
New Member
Posts: 2
Joined: Mon Dec 15, 2008 10:11 am

Re: Assign the new SEO rel="canonical" link tag in HEAD of page

Post by Bully »

i like the UDT approach on this, thanks mww!

There is only one issue i have with this.
I'm using the MLE Version of cmsms, so the standart start page has the canonical url http://www.domain.de/index.php?page=home&hl=de_DE .
I'd like to have www.domain.de in the index instead of the above canonical.
Any idea on this without me to specify an extra template to the startpage?
Pretty urls on the german pages would be nice too, but this collides with the mle stuff.

TIA
Bully
Last edited by Bully on Sat Feb 14, 2009 4:48 pm, edited 1 time in total.
calguy1000
Support Guru
Support Guru
Posts: 8169
Joined: Tue Oct 19, 2004 6:44 pm

Re: Assign the new SEO rel="canonical" link tag in HEAD of page

Post by calguy1000 »

Code: Select all

<link rel="canonical" href="{$content_obj->GetURL()}">
nuf said
Follow me on twitter
Please post system information from "Extensions >> System Information" (there is a bbcode option) on all posts asking for assistance.
--------------------
If you can't bother explaining your problem well, you shouldn't expect much in the way of assistance.
SimonSchaufi

Re: Assign the new SEO rel="canonical" link tag in HEAD of page

Post by SimonSchaufi »

calguy1000 wrote:

Code: Select all

<link rel="canonical" href="{$content_obj->GetURL()}">
nuf said
and XHTM valid:

Code: Select all

<link rel="canonical" href="{$content_obj->GetURL()}" />
;)
JohnnyB
Dev Team Member
Dev Team Member
Posts: 731
Joined: Tue Nov 21, 2006 5:05 pm

Re: Assign the new SEO rel="canonical" link tag in HEAD of page

Post by JohnnyB »

calguy1000 wrote:

Code: Select all

<link rel="canonical" href="{$content_obj->GetURL()}">
nuf said
Please explain what that is in reference to because adding this to a template produces a fatal error:
Fatal error:  Call to a member function GetURL() on a non-object in /home/account-name/public_html/tmp/templates_c/%%79^79E^79E2D318%%template%3A24.php on line 32
I tried a few alternative ways and even thought maybe you had a cgsimple smarty call for it like

Code: Select all

<link rel="canonical" href="{$cgsimple->GetURL()}">
Bully wrote: There is only one issue i have with this.
I'm using the MLE Version of cmsms, so the standart start page has the canonical url http://www.domain.de/index.php?page=home&hl=de_DE .
I'd like to have www.domain.de in the index instead of the above canonical.
Any idea on this without me to specify an extra template to the startpage?
Pretty urls on the german pages would be nice too, but this collides with the mle stuff.
Looks like you are using SEO URLs - does the canonical UDT generate this URL http://www.domain.de/index.php?page=home&hl=de_DE ?? 

You can specify any real URL as the canonical so, you can specify http://www.domain.de/de/ as the canonical URL
Maybe you can expand the UDT to test for certain pages, and supply a custom canonical tag for those pages
Last edited by JohnnyB on Sun Feb 15, 2009 8:37 am, edited 1 time in total.
"The art of life lies in a constant readjustment to our surroundings." -Okakura Kakuzo

--
LinkedIn profile
--
I only speak/write in English so I may not translate well on International posts.
--
JohnnyB
Dev Team Member
Dev Team Member
Posts: 731
Joined: Tue Nov 21, 2006 5:05 pm

Re: Assign the new SEO rel="canonical" link tag in HEAD of page

Post by JohnnyB »

Bully wrote: i like the UDT approach on this, thanks mww!

There is only one issue i have with this.
I'm using the MLE Version of cmsms, so the standart start page has the canonical url http://www.domain.de/index.php?page=home&hl=de_DE .
I'd like to have www.domain.de in the index instead of the above canonical.
Any idea on this without me to specify an extra template to the startpage?
Pretty urls on the german pages would be nice too, but this collides with the mle stuff.

TIA
Bully
I'm mot sure if I understand because you are using SEO friendly URLs and you can specify the URL you want to in the canonical tag for the search engines.  So the URL can be http://www.domain.de for the index page.

Do you mean to say that the above code generates this URL http://www.domain.de/index.php?page=home&hl=de_DE because of MLE ?
"The art of life lies in a constant readjustment to our surroundings." -Okakura Kakuzo

--
LinkedIn profile
--
I only speak/write in English so I may not translate well on International posts.
--
JohnnyB
Dev Team Member
Dev Team Member
Posts: 731
Joined: Tue Nov 21, 2006 5:05 pm

Re: Assign the new SEO rel="canonical" link tag in HEAD of page

Post by JohnnyB »

I edited my first attempt to test if the page exists first because if bad request normally gave a 404 error, this UDT broke it and gave a fatal error(Call to a member function GetURL() on a non-object in).

Modified to prevent that by testing if the page exists first.

Maybe there is a better way to test for 404's...? It doesn't break my 403's - my 403 error page is shown, although the canonical link is http://www.my-domain.com/forbidden403 not sure if that is a big deal for Search Engines.

Code: Select all

global $gCms;
if ($gCms->variables['page_name'] !='') {
	$manager =& $gCms->GetHierarchyManager();
	$node =& $manager->sureGetNodeByAlias($gCms->variables['page_id']);
	
	$content =& $node->GetContent();
	if (isset($content) && is_object($content))
	{
		if ($content->GetURL() != '')
		{
		echo '<link rel="canonical" href="'.$content->GetURL().'" />';
		}
	}
}
"The art of life lies in a constant readjustment to our surroundings." -Okakura Kakuzo

--
LinkedIn profile
--
I only speak/write in English so I may not translate well on International posts.
--
Russ
Power Poster
Power Poster
Posts: 813
Joined: Fri Nov 25, 2005 5:02 pm

Re: Assign the new SEO rel="canonical" link tag in HEAD of page

Post by Russ »

OK mww that works for normal pages and 404's but not for modules like news or calendar or album. You just get the root url e.g. http:www.domain.com/news/

Russ
JohnnyB
Dev Team Member
Dev Team Member
Posts: 731
Joined: Tue Nov 21, 2006 5:05 pm

Re: Assign the new SEO rel="canonical" link tag in HEAD of page

Post by JohnnyB »

figures.

My short solution is not to use it on those pages by testing if news or the other modules are being viewed. something like {if $page_id !='news'} {canonical} {/if}

but in the long term that is not an acceptable solution, nor is the way the other modules can not generate unique title tags with out smarty tricks.

When I get time, I'll try incorporating one of the tips/tricks floating around that help get unique title tags for the news module and apply to the URL.  not sure when I will though...

this is a problem that could be solved by someone more intimate with CMSMS and smarty. unfortunately I'm just a HTML/CSS guy that does not know enough to get this done easily. ugh.
"The art of life lies in a constant readjustment to our surroundings." -Okakura Kakuzo

--
LinkedIn profile
--
I only speak/write in English so I may not translate well on International posts.
--
Russ
Power Poster
Power Poster
Posts: 813
Joined: Fri Nov 25, 2005 5:02 pm

Re: Assign the new SEO rel="canonical" link tag in HEAD of page

Post by Russ »

Also mww it runs a bit deeper, with no standard 'nice' url format and no conventions for this to work across differing modules. Each module seems to do its own thing, so you could add smarty traps for each module you use, news, album,calendar etc? But I don't think this is how it should be?
Russ
Power Poster
Power Poster
Posts: 813
Joined: Fri Nov 25, 2005 5:02 pm

Re: Assign the new SEO rel="canonical" link tag in HEAD of page

Post by Russ »

I have been thinking abut getting Canonical link rel's to work with modules and here it is, with some provisos, and on the work that others have previously done. Note I'm no Smarty expert!

1. In the News Details template:

Code: Select all

{assign var="newsarticle" value=$entry->title}
{assign var="entryid" value=$entry->id}
You can add these to other templates for e.g. Calendar or Album. However they will need some alteration for the module variables.

2. In your Page Template before you set the canonical link rel:

Code: Select all

{capture assign='newscontent'}{content}{/capture}
{if isset($newsarticle)}
{if ($page_alias eq 'news')}
{capture assign='ModuleCanonical'}<link rel="canonical" href="{root_url}/{$page_alias}/{$entryid}/{$page_id}/{$newsarticle|replace:" ":"-"}/" />{/capture}
{/if}{/if}
You can ignore this
Again I guess you need different captures for different modules. Also if your url is in a different format or just a different module, like for Album or Calendar, then you need to do a bit of editing of the 'ModuleCanonical' assignment. (I know for some modules that you can add ready made variables for the ModuleCanonical link - but I broke it down so people could see what was happening. I am also not sure you still need the '{capture assign='newscontent'}{content}{/capture}' stuff with the new CMCMS and reading of the whole page - but it works and perhaps someone could suggest a better way of doing it?
I also think maybe having lots of
{capture assign='newscontent'}{content}{/capture}
....
and
{capture assign='eventcontent'}{content}{/capture}
...
is going to slow things down? Especially if you need one for News/Calendar/Album/ etc. ???)

I would also limit where you are want the main news to be, the '{if ($page_alias eq 'news')}' bit. For example, if your news index is off the root then fine and dandy, but if you also put it on you home page - what happens - look at the links - click them and then look at the Canonical url in the source - you can easily end up with invalid URLs there! For news at least, it is best to wrap with a page alias check.
Also maybe hard code your return id to you main news page id
e.g in the News module action.default.php on line six add
$returnid=82; (or whatever your main news page id is.)
It is clumsy - but works -anyone know a better way?

You could also capture/change/write the page title in the template with this smarty, just a few extra lines...


3. And where you want the Canonical link to appear in your Page Template, below the above smarty code;-)

Code: Select all

<!-- SEO Canonical  -->
{if isset($ModuleCanonical)}{$ModuleCanonical}
{else}{GetCanonical}
{/if}
4. This above assumes you have a UDT Called 'GetCanonical' or you can change the name for whatever you like.
UDT from above post:

Code: Select all

global $gCms;
if ($gCms->variables['page_name'] !='') {
	$manager =& $gCms->GetHierarchyManager();
	$node =& $manager->sureGetNodeByAlias($gCms->variables['page_id']);
	
	$content =& $node->GetContent();
	if (isset($content) && is_object($content))
	{
		if ($content->GetURL() != '')
		{
                   echo '<link rel="canonical" href="'.$content->GetURL().'" />';
  
		}
        
	}
}
Comments and suggestions welcome.
Russ
Last edited by Russ on Fri Feb 20, 2009 10:18 am, edited 1 time in total.
JohnnyB
Dev Team Member
Dev Team Member
Posts: 731
Joined: Tue Nov 21, 2006 5:05 pm

Re: Assign the new SEO rel="canonical" link tag in HEAD of page

Post by JohnnyB »

@russ - good effort and work trying to figure this out. 

I went back to Calguy's post above

Code: Select all

<link rel="canonical" href="{$content_obj->GetURL()}" />
This WORKS! in CMSMS vers 1.5 and newer.  I'm not sure why I was getting an error when I first tested, but it works on a another site using 1.5.2.  I have not tested for news and other modules yet and will as soon as I get the chance.

My plan is to include this in the template(s).
"The art of life lies in a constant readjustment to our surroundings." -Okakura Kakuzo

--
LinkedIn profile
--
I only speak/write in English so I may not translate well on International posts.
--
JohnnyB
Dev Team Member
Dev Team Member
Posts: 731
Joined: Tue Nov 21, 2006 5:05 pm

Re: Assign the new SEO rel="canonical" link tag in HEAD of page

Post by JohnnyB »

Ok so it does not work with the News module and I suspect no other modules either... :(
I went back to Calguy's post above
Quote
Code:

GetURL()}" />

This WORKS! in CMSMS vers 1.5 and newer.  I'm not sure why I was getting an error when I first tested, but it works on a another site using 1.5.2.  I have not tested for news and other modules yet and will as soon as I get the chance.
"The art of life lies in a constant readjustment to our surroundings." -Okakura Kakuzo

--
LinkedIn profile
--
I only speak/write in English so I may not translate well on International posts.
--
viebig

Re: Assign the new SEO rel="canonical" link tag in HEAD of page

Post by viebig »

Hi All,

I think that canonical urls for CMSMS are useful for a static sites using just the content module.

Im used to create one page like 'news' and set all the news using the detail page. The canonical URL will be the same for all news. This will confuse or limit the search engine approach on site. And for any other module it wont work too, since most of then uses the current page alias to display detail views.

I think CMSMS is fine with search engines the way it is. Of course we can improve it, using methods described in Tips ń Tricks section.

As calguy and other developers mentioned before on many discussions, we agreed that the best thing you can do is prevent the search engines to index your site before the definition of the url scheme and/or other tweaks.

Google wont penalize you for duplicated content, but probably will make your pages less relevant or simply exclude some of then  from the standard listing.

We have to find a balance between fancy things and usability, acessibility and SEO. Content is king always, title and meta description are really important, pretty urls are one step after.

Hope that helps!

Regards
Last edited by viebig on Sat Feb 21, 2009 3:58 am, edited 1 time in total.
Locked

Return to “Tips and Tricks”