Canonical URL tag, RubyOnRails, and why you are stupid for not using it

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:

  1. Paperclip before_process For Your Habitual Pornographic Needs
  2. SearchLogic Just Seems … Logical …
  3. Ezmobius Is The Devil And Is Gaining Followers For The Apocalypse Using Nanite
  4. Monkey Patching RubyOnRails Caching For Debugging
  5. SEO vs Social Networking
  • Dan
    "If this is hard to understand, then I feel sad for your mother."

    hahaha nice... here's my question though. For your example above, if you use /category/seo/canonical-yadda-yadda/ as the canonical url, are you forfeiting the 'rubyonrails' keyword from the other url?
  • I believe so since the canonical url tells the spider of the real location.
  • stephen_murdoch
    thanks for the post, it was helpful

    do you recommend any plugins for creating categories? currently I'm using awesome_nested_set...
  • Unless you have thousands of categories, acts_as_tree will be fine, otherwise, the *nested_set plugins would be advised.
  • nice.
blog comments powered by Disqus