What is a Canonical URL Tag? Simple. It’s a tag you put in your HTML to let the spiders know that the page it is crawling is a duplicate of another page on your site. “But Kris, why would should I worry about this?” Simple. Your site idea is probably stupid and will not catch on socially, thus, you have to rely on the search engines to deliver you traffic. If the search engines see your content duplicated, it will get confused on which page to index, you will suffer lower rankings and relevancy. How does one implement it? Simple.
1 | <link rel="canonical" href="http://www.seomoz.org/blog" /> |
Now, what does this have to do with Rails? Nothing, except for the fact there is a neat little plugin for it written by mbleigh called, you guessed it, canonical-url. It’s quite simple to use. Say for example I have a blog with categories, all of which have posts. If a post belongs to multiple categories, I would want to use this in the show action to point to the main article.
1 2 3 4 | def show @post = Post.find(...) canonical_url post_path(@post) end |
Then in your header, you would put:
1 | <%= canonical_link_tag %> |
It can’t be simpler. It canonical_link_tag defaults to the current request uri. If you don’t implement this, your genitals will be infested by the fleas of a thousand camels.
updated
Someone with a crappy site idea messaged me asking for specific details, as if my categories example wasn’t enough. So let me go into more details. If an article is posted in multiple categories with the same content:
1 2 | /category/rubyonrails/canonical-yadda-yadda/ /category/seo/canonical-yadda-yadda/ |
The search engines wouldn’t know which is the real content. This leads to a negative rating on your site for having duplicate content. So, by pointing the copies at the real version, you won’t get penalized, as the search engines would know it’s a copy. If this is hard to understand, then I feel sad for your mother.
Related posts: