Inbound Marketing Tips and Tricks

SEO for Ruby on Rails (Complete Guide)

I was surprised to see that RoR being such a popular framework has very few gems and documentation to make the sites more search engine friendly so I thought to write one for inBoundio as most of the results that come with Google are stale and outdated. 

1. Clean Urls - RoR by default, comes with clean urls which is nice but still you certainly want /articles/marketing-tutorial instead of /articles/22, right ? 

One of the easiest way to add titles in url is to override the to_param method in the model whose URLs we want to change,

## /app/model/article.rb

class Article < ActiveRecord::Base
  def to_param
    "#{id} #{name}".parameterize

Please check the excellent railscast by Ryan Bates to understand it better. Though the above method will still be having the id, if you want proper clean urls, you have to use standard gems. For slugs and permalinks, you can use friendly_id

2. Redirection of /page/index to /page/ - You also want to redirect /page/index pages to /page. You can do this by using simple redirect in routes.rb file

get '/page/index' => redirect('/page')

There is an excellent tutorial on redirection here 

3. Put Title and Description (Meta tags) on pages - RoR do have a Meta tags gem which takes care of meta tags as well as noindex, nofollow and canonical urls issues. 

If Meta Tags looks too heavy, you can use metamagic gem which is more light weight with basic title, description and keywords options. 

4. 301 Redirect from non www to www - On your production server, most likely you are using apache + phusion passenger, so in your /etc/apache2/sites-available/default file, you can have following code (most of it must already be there, the key is ServerAlias)

DocumentRoot /var/www/public
AllowOverride all
Options Indexes FollowSymLinks MultiViews

You can see more examples of 301 redirection at RubyonRails API documentation 

5. Robots.txt - Rails doesn't come up with any robots.txt so you do want to create one in /public folder of your app. Here is a sample robots.txt of your rails app can be -

Disallow: page/*/edit # or whatever the name of resources are Disallow: admin/* # if you are using active-admin 

6. Redirect older urls to new urls - If you have changed the urls of an older post to new, you can edit routes.rb to do 301 redirect from older urls to new once.
match "/stories" => redirect("/posts")

Unfortunately rails lack a better way to do mass edit/redirection though you can build logic in routes file. 

7. Add noindex - If you don't want search engines to index some pages, you can also use noindex tag. Meta_tags gem looks be the easiest way to add noindex tag

8. Sitemap and XML Sitemap - for simple sitemap, you can use sitemap gem or Dynamic Sitemaps and for XML Sitemap, you can use XML Sitemap gem. 

9. Breadcrumbs - To get breadcrumbs, you can use Gretel gem. 

Advanced SEO for Ruby on Rails 

1. i18n - Internationalization - If your target audience is not english and if your content is in other language, you can use i18n which is supported in rails out of the box. 

RoR provides very convenient way to replace strings using config/locales/xx.yml file. For example, if you want to convert your site from english to spanish, put an es.yml file in your locale folder

## config/locals/en.yml


title: "Spanish Name of Product"
description: "This is Spanish Description of the product"

## application_controller.rb

before_filter :set_locale

def set_locale

I18n.locale = params[:locale] if params.include?('locale')

## layouts/application.html.erb


<%= link_to_unless_current "English", locale: "en" > |

<%= link_to_unless_current "Spanish", locale: "es" >

## products/index.html.erb

<%= t ".title" %>

<%= t ".description" %> ... <%= Product.human_attribute_name(:price) %>
## config/routes.rb

scope ":locale", locale: /#{I18n.available_locales.join("|")}/ do

resources :products

root to: 'products#index'


match '*path', to: redirect("/#{I18n.default_locale}/%{path}"), constraints: lambda { |req| !req.path.starts_with? "/#{I18n.default_locale}/" }

match '', to: redirect("/#{I18n.default_locale}")

2. Optimize site for speed - You can use caching to speed up the site loading as it do play a very important part in site ranking. With RoR, you can do caching at two levels, server-side and client side, though they both are mammoth topics, let me try to cover at least the basics. This should be enough for you to get going. 

Server Side Caching - You can enable server-side caching by

config.action_controller.perform_caching = true

after this, you can add caching for any action

caches_page :index, :show

def index



def show



Client Side Caching
- For client side caching, do make sure you are using CDN to deliver jQuery, bootstrap and similar libraries. Also make sure that assets are precompiled (by default, in products, assets are precompiled anyway). If it is not, you can use following settings
config.serve_static_assets =true

config.static_cache_control ="public, max-age=2419200" # cache will get updated every four weeks

You can also use additional caching at http level using rack-cache gem. 

3. GeoTargeting - You can use geocoder or geokit gems to use geotargeting on your content. Geokit offers multiple option for verifying geolocation liking yahoo geo API where as with geocoder, you can install all the latitudes/longitudes in a database table. After this, you can easily pull user location information by following calls.

geocoded_by :full_street_address # can also be an IP address

after_validation :geocode # auto-fetch coordinates

This can be used for redirections and showing different content to different countries. 

4. Optimization for Mobiles - As per SearchEngineLand, mobile SEO will play a big role in search results on mobiles. 

To optimize your site for mobiles, you can use mobile_fu gem using which you can detect user agents and can show respective stylesheets for the mobile type like iPhone, Blackberry, Nokia, android etc. Add this line in controller
class ApplicationController < ActionController::Base


and then add the line below to config/initializers/mime_types.rb
Mime::Type.register_alias "text/html", :mobile

after this, you just need to call

to detect if the referer is mobile device and if it do, you can render mobile view. 

If you don't want to do all this, at least make sure you are using responsive designs so the display gets auto adjusted for mobiles and tablets. 

Here is an infographic to summarize the whole post (well at least the beginners part, it was difficult for me to get everything on a single infographic). 
If you have any SEO related suggestions or want to suggest code snippets and gems for SEO and Marketing, please post them in comments.


Previous Article 5 Free Email and Newsletter Templates by inBoundio
Next Article 8 Drupal modules for Inbound Marketing



InBoundio is the world’s simplest inbound marketing software targeted towards individuals, small businesses looking to organize their Internet Marketing efforts as well as Marketing or Design Agencies looking to offer an easy to use white label / reseller service to their clients.

Offering Social Media Marketing, Landing Pages, CRM, Drip Marketing, Email Marketing, Autoresponders, Reporting and Analytics, InBoundio has all the essentials you need in one simple, powerful and affordable solution.


Enter the code shown above in the box below.