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?
TestLink.zip (13.98 kb)