CodersBarn.com
The ASP.NET Community Blog

ASP.NET SEO and the Canonical Tag

February 21, 2009 08:03 by agrace

Canonical Tag Recently, Google, Microsoft and Yahoo announced support for a new canonical tag in an effort to combat duplicate urls (read duplicate content) on websites. Quite simply, you can add an HTML <link> tag to the <head> section of your page to indicate the preferred, or canonical, version of the page url.

If you have something like this:

http://www.mysite.com/products.aspx?productid=123

 

or

http://mysite.com/products.aspx?productid=123

 

You can have the search spider interpret it as this:

http://mysite.com/products.aspx

 

It works like a 301 redirect and is just a hint to the search spider. In other words, the other search engines are free to recognize it or not. It's mind-boggling the amount of work developers have had to do to get around this problem up to now, and it was this simple to fix at the end of the day?

To implement this for your ASP.NET products page, with its GridView of pageable, sortable widgets, you could do the following:

using System;
using System.Web.UI;
using System.Web.UI.HtmlControls;

public partial class Products : Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        HtmlLink canonicalTag = new HtmlLink();
        canonicalTag.Href = "http://mysite.com/Products.aspx";
        canonicalTag.Attributes["rel"] = "canonical";
        Page.Header.Controls.Add(canonicalTag);
    }
}

 

ASP.NET renders the following: 

<head>
    <title>Products Page</title>
    <link href="http://mysite.com/Products.aspx" rel="canonical" />
</head>

 

There is only one problem with this if you are using a XHTML doctype declaration; per the W3C recommendation, a closing slash in the <link> tag is illegal. The correct format is:

    <link href="http://mysite.com/Products.aspx" rel="canonical">

 

So where does this leave us? For me, relaxing the doctype to anything less than XHTML transitional is not an option. Does this mean we have to use an HtmlTextWriter to customize the output for this particular tag or is there some easier way? Has anyone got a suggestion or will we have to wait for a fix?

kick it on DotNetKicks.com


Tags: ,
Categories: ASP.NET | SEO
Actions: E-mail | Permalink | Comments (14) | Comment RSSRSS comment feed

Comments

February 24. 2009 21:48

trackback

Trackback from DotNetKicks.com

ASP.NET SEO and the Canonical Tag

DotNetKicks.com

February 24. 2009 21:59

trackback

Trackback from Web Development Community

ASP.NET SEO and the Canonical Tag

Web Development Community

February 24. 2009 23:49

trackback

Trackback from DotNetShoutout

ASP.NET SEO and the Canonical Tag

DotNetShoutout

February 25. 2009 09:27

trackback

Trackback from progg.ru

ASP.NET SEO and the Canonical Tag

progg.ru

February 26. 2009 03:57

trackback

Trackback from ASP.NET SEO and the Canonical Tag - DotNetBurner

ASP.NET SEO and the Canonical Tag

ASP.NET SEO and the Canonical Tag - DotNetBurner

February 27. 2009 04:44

Andrew Nurse

Cool stuff!  I'm not sure about the XHTML compliance issue though.  The link you provided is to the HTML 4.01 spec, but some quick tests on the W3C Validator (http://validator.w3.org/check) seemed to indicate that XHTML 1.0 (Transitional/Strict) and XHTML 1.1 both support an empty "<link />" tag.

Andrew Nurse

February 27. 2009 04:45

Andrew Nurse

The link in the above post should be http://validator.w3.org/check

Can't edit the comment :S

Andrew Nurse

February 27. 2009 04:58

agrace

Thanks for pointing that out Andrew, good news indeed Smile Like, we don't have enough to contend with, with IE8 coming out any day now?!
Anthony Smile

agrace

March 5. 2009 03:45

trackback

Trackback from 朱博

Canonical URL Tag - 继SiteMap之后最重要的搜索引擎优化实践

朱博

July 30. 2009 11:24

Chris

What would be the next step in programmatically adding the tag to each page/post in BlogEngine.net?

Chris

August 27. 2009 22:48

eiretics

The <link> element should be self closing in xHTML, ie. <link href="http://mysite.com/Products.aspx" rel="canonical" />

In HTML 4 it is not self closing.

eiretics

July 5. 2010 11:19

seo indore

I am glad to see that some webmasters are still there who share there valuable suggestions to the visitors.

Thanks for the resource.

seo indore

July 31. 2010 01:45

rt

grgrgregregregregregregreg

rt

July 31. 2010 01:46

rt

fake tissot watches <a href=www.buyfakewatch.com/ladies_watch.html>ladies watches</a> summit watches

rt

Add comment




  Country flag

biuquote
  • Comment
  • Preview
Loading