24. Oktober 2013
Clones and Canonical-Tags - how to prevent duplicate content
This is a tricky one! As you may know, you can use a canonical tags in the header to prevent duplicated content on your page. On the other hand, Sitecore has a nice feature since version 6.4, the cloning of items. But if you clone a content item in your content tree you'll produce duplicated content.
So far so good. The item knows that it is cloned and knows the source, of course. So the canonical tag links to the source item … But I told you, this is a tricky one, didn’t I?
When publishing the item, Sitecore removes all the important information. Then the item is technically uncloned and the canonical tag can’t be determined anymore.
So John West had to save my day once more… ;)
I amended his code slightly, because I think it is better to only implement one return per method. In addition to that there were some more requirements from our side:
- If the cloned item refers to a different version of the source item, it’s content isn’t a duplicate.
- Because this is a multi-site and multi-language solution, the canonical item has to be in the same site tree (each site has its own content tree)
So I decided to use a VersionedLinkField, because here we have database, ItemId, language and version stored. The __Source Field is of this kind too, so I can just copy the string and change the DB name.
The using of an ItemUri object makes it quite easy to get the ItemId, the language and the version. These methods are placed in a class that extends the Item API like mentioned in John’s blog post.
Finally, for the canonical tag itself, you have to check in your layout (or put it in an own control) if it is a clone item, create and write the link.
Take care that your Link-Manager is configured well to get SEO friendly URLs
Good Luck and Thank you John