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,

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

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)

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<br> 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.

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

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

after this, you can add caching for any action

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

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.

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

and then add the line below to config/initializers/mime_types.rb

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.

  • quentin langeveldt

    Thank you for this information. it looks very promising. =)

  • D瓜哥

    Thank you. Mark

  • Alossia

    Thanks for this useful post! I’ve bookmarked for future reference. Another site that I also recommend that you haven’t included in your post is http://www.dreame.biz. I love it because it has more than 50+ Tutorial Best.Thank you

  • Bá Tới

    Thank you for this information.

  • Inês Rodrigues

    this is great! 🙂 thank you very much

  • hackhowtofaq

    If anybody is interested I have made an updated SEO checklist with my findings…

    Sorry for spamming here but the SO post is closed

    Here is the link

    http://www.railscook.com/pro_recipes/rails-seo-optimization-checklist/

    Ty

  • adriennhem

    Good stuff man, thanks!

  • Please republish the infographic since the image is broken!!! I can’t see it please!

  • sirjustinwhite

    Question about your blog… how do you handle the syntax highlighting? I really like that, and would like something similar!