SEO tip: Canonical link elements in Kentico CMS
The canonical link element is a feature which Google, Microsoft and Yahoo announced together to provide a better SEO options. Even that Kentico CMS does not support it directly yet, you can quite easily make this happen. Check out how ...
Hi there,
Simply said, canonical link element is a tag which allows you to make the page to behave as a page under different URL no matter where the page is or which parameters it. With this, you can join together several pages for SEO reasons. Something similar is possible with our new
SEO settings in 5.0, but they are probably not as powerful as these canonical link elements.
We didn't go that far with our web sites yet so I cannot validate how useful these canonical links really are but they seem like a decent feature.
Before you continue further, it is necessary that you know how they work, so if you are not yet familiar with them, see
the video from Matt Cutts (about 20 minutes).
You should also be familiar with the
Macros in Kentico CMS.
What options do you have?
In this article, I will describe you two basic options you can use for your canonical links:
-
First one will not require any programming and will purely use macros and standard Kentico CMS UI to make the canonical links to be specified by the page editor (which may be also useful in some scenarios).
-
With the second one, I will use custom macros with a custom macro handler to allow the canonical links to be generated automatically. This will be just a few lines of code, you just need to know where to put it and how.
Canonical links defined by the page editor
To allow editor edit the canonical link, you need to add some field for it first, so just go to the Site manager -> Development -> Document types, and add some text field to any document where you want to allow this. I recommend you to share the name for it across all Document types, so you can use just single macro for it.
I used mine in Blog post document type and name it
CustomCanonicalLink (text, 200 characters)
NOTE: You may need to allow the empty value for the field if you do not require your editors to enter the canonical link URL or modify some predefined document types such as
CMS.MenuItem (which have their own custom inserting page).
Here is what editor can see, I will just enter the main document URL because I want all possible aliases of this document to behave as this main URL of the document (from the SEO perspective).
Now I need to use some macros as the site designer to allow this data to display where I need. Go to the Design tab of the root page (I want to include this to the master page because I want it to be supported on any page). Then add the web part
Head HTML code because we want to add some head tags.
You just need to set two of it's properties:
-
HTML code - You just need to enter the canonical link HTML with the link value injected through a macro:
<link rel="canonical" href="{%CMSContext.CurrentDocument.CustomCanonicalLink%}
(we are comparing the value against empty string, it gives true if the string is not empty)
-
Enabled - You need to click on the small arrow to edit the macro. We need this code to be displayed only when there is some value available, enter macro:
So when the link URL is set, the canonical link is displayed. And that's it. If you look at the page which has the canonical link field set, you can see in the code of the web page that there is a canonical link:
Now let's look at automatic canonical links.
Automatic canonical links
This will be quite similar yet different. We will use the same web part but this time a custom macro to populate the value of canonical link.
Add the Head HTML web part on the master page and set its HTML code property to value:
{#GetCanonicalLink#}
This is the name of custom macro we will implement. You don't need to do any changes to the Enabled property since the macro code will ensure empty value if the data is not available.
Now open the file ~/App_Code/Global/CMS/CMSCustom.cs, locate the method ResolveCustomMacro and replace it with following code:
using CMS.TreeEngine;
using CMS.PortalEngine;
...
///
/// Custom macro handler
///
///Sender (active macro resolver)
///Expression to resolve
///Returns true if the macro matches (was resolved)
public static string ResolveCustomMacro(MacroResolver sender, string expression, out bool match)
{
match = false;
string result = expression;
// Add your custom macro evaluation
switch (expression.ToLower())
{
case "getcanonicallink":
PageInfo currentPage = CMSContext.CurrentPageInfo;
match = true;
return "<link rel=\"canonical\" href=\"" +
TreePathUtils.GetUrl(currentPage.NodeAliasPath, currentPage.DocumentUrlPath) + "\" />&quo&;;
}
return result;
}
In this case, the custom macro provides you with the complete tag for canonical link. I used current page info to get the main URL of the current document but you may generate just&&;any link.With this, you automatically get the canonical link for just any document using that master template.
You can very easily control where to display it through web part properties or place the web part only on specific templates.
What next?
The next step is that you decide which way you want to go or even combine them, and let me and others know what is your experience with it.
See you next time.