Article / November 20, 2008

Rails SEO Starter Guide

By Dejan Simic/3607 Views/5 Comments

Couple days ago Google published SEO Starter guide. It's a short document but packed with useful information, that every web developer should check out, especially ones new to SEO.

This article will cover on how the developer related tips in that document apply to a Rails web app.

Create unique, accurate page titles


Ideally, you should create a unique title for each page on your site.

So forget about a static value for title tag in your template layout file (ie. views/layouts/application.html.erb). Title tag value must be content related.

Code sample bellow will include setting meta tag for description as well, which is also important part of search engine optimization. Read more about importance and good choices for title and description values in Google SEO Starter guide.

There's a great screencast on how to approach implementing this, presenting couple of options. My preferred way is with content_for.

In some show.html.erb have something like:

  <% content_for :title, "Rails SEO Starter Guide" %>
  <% content_for :description, "Learn how to apply Google's SEO Starter guide in Rails apps." %>
  
  <h1><= yield :title %></h1>

... and in application.html.erb:

<head>
  <title><%= yield(:title) || "ELC Technologies" %></title>
  <%= tag(:meta, :name => 'description', :content => yield(:description) || "Global Web Development Agency")  %>
</head>

Improve the structure of your URLs

  • Use permalink_fu plugin for pretty permalinks. "Default" Rails URLs with numeric IDs (ie. /articles/1705) is a terrible choice for many reasons for pages with content valuable to user.
  • As the site grows, sometimes URLs has to be changed to maintain well structured URLs. In that case, you must make sure that "old" links are redirected properly, by using 301 moved permanently status.

    def old_show
      redirect_to params.merge(:action => :show), :status => :moved_permanently
    end
    

Optimize your use of images

Always set image :alt value for images that can be considered as "content" (ie. not part of design).

<%= image_tag photo.url, :alt => "spider-man" %>

Be aware of rel="nofollow" for links


When would this be useful? If your site has a blog with public commenting turned on, links within those comments could pass your reputation to pages that you may not be comfortable vouching for. Blog comment areas on pages are highly susceptible to comment spam. Nofollowing these user- added links ensures that you're not giving your page's hard-earned reputation to a spammy site.

So make sure you use this if you use auto_link for user generated content such as comments:

<%= auto_link(h(comment.body), :all, :rel => "nofollow") %>

Take advantage of web analytics services

Put the needed javascript code in partial, so that you don't clutter application layout template. Also, you can skip rendering it in development mode since you don't need useless JavaScript code executed on every page refresh while developing.

<%= render(:partial => "layouts/analytics") unless Rails.env == "development" %>

If you're using GoogleAnalytics, you can even use it as an engine for finding out popular content on the site.

Comments

Posted by Eugenia Jongewaard on about 1 year ago46f61a8a15c5e234ad416b590a2676c9?s=30

Nice post! this is very important to improve accessibility too.

Posted by Dejan Simic on about 1 year ago46f61a8a15c5e234ad416b590a2676c9?s=30

Thanks! Indeed it is.

Posted by Daddy Cooking on 11 months ago683e0e2aa3dec01f480c26cdff19a6e1?s=30

I haven't found a lot of help out out to incorporate the 'nofollow.' It seems that it can be

link_to "abc site name", :rel => 'nofollow'
. Anyway, not sure that the a blanket 'nofollow' strategy should be followed--why not reward commenters if they have taken the time to comment?
Posted by Dejan Simic on 11 months agoDe0ba549099a6d7e54e41edf73777d7d?s=30

Of course, that's a matter of preference. To me, reward is the link itself, since it will directly drove some traffic to commentator's site.

Anyway, thanks for the link_to example.

Posted by Alexey Poimtsev on 7 months ago561f6b93e60563dabb66fb9802d06fa3?s=30

I can recomend to use http://github.com/norman/friendly_id/ plugin for generating permalinks

Add a Comment