Stefan Pienaar
I would love to change the world, but they won't give me the source code

ASP.net and the Canonical Tag

March 10, 2009 18:50 by stefanpienaar

The major role players in the Search Engine world (Google, Yahoo and Microsoft) recently announced support for the canonical tag. What this means is you now have a way to *hint* the crawler to the best version of the current page.

Because “http://www.stefanpienaar.co.za”, “http://stefanpienaar.co.za” and “http://www.stefanpienaar.co.za/Default.aspx” are seen as different pages (with duplicate data) by search engine crawlers, we can now add the canonical tag to a page’s head tag to indicate which one should be used.

For example for my blog I could add the following tag between my opening and closing head tags:

<link href="http://www.stefanpienaar.co.za" rel="canonical" />

This way even if someone incorrectly links to “http://stefanpienaar.co.za”, the search crawler will now know that they are in fact the same page and which one I prefer.

To get your asp.net page to work with the canonical tag is really simple:

1. Change your <head> tag to include runat=”server” if it doesn’t already.

<head runat="server">
    <title>My Site</title>
</head>

 

2. From your page load event, create a new HtmlLink object and add it to the Page’s header:

HtmlLink lnkCanonical = new HtmlLink();
lnkCanonical.Href = "http://www.stefanpienaar.co.za";
lnkCanonical.Attributes["rel"] = "canonical";
Page.Header.Controls.Add(lnkCanonical);

Categories: .net
Actions: E-mail | Permalink | Comments (0)