Sitemaps On Rails

By eric

SEO being an interest of mine, I couldn’t quite wrap my head around releasing a webapp without a sitemap. The problem is that there aren’t any really great sitemap plugins for Rails. Now I will grant that creating a sitemap in Rails is a challenging proposition and one that I would not like to undertake on my own unless absolutely necessary. But I was hoping that there would be a rails sitemap “killer app” like there is with almost everything else in Rails.

So I dove in and tried a few options until I found one that worked. First I wrote some code to generate an XML file and then created a sitemap_index.xml.gz file by hand. This was very kludgy and definitely not a permanent solution. I had also read suggestions about doing it in a sitemap_controller.rb file, but that seemed just as kludgy as using a view to generate the XML. It was then time to explore the plugin world.

After trying a few random plugins that I stumbled across, I finally found one that works for me. It’s call sitemap_generator by Adam Salter. What I really like about it is that it supports actually Ruby code in the config/sitemap.rb file. Additionally, it solves a few common sitemap problems from the getgo (taken from the github README):
* Support for more than 50,000 urls (using a Sitemap Index file)
* Gzip of Sitemap files
* Variable priority of links
* Paging/sorting links (e.g. my_list?page=3)
* SSL host links (e.g. https:)
* Rails apps which are installed on a sub-path (e.g.

Using this plugin is easy. Install it just as you would install any other plugin or gem (instructions are in the README). Getting right into the sitemap, it’s really easy to accomplish whatever you want to do using regular Ruby code. For example, if I simply want to find all the businesses on the site and add them to the sitemap, I can do this:

Business.find(:all).each do |biz|
  sitemap.add business_path(biz), :lastmod => biz.updated_at,
               :priority => 0.9, :changefreq => 'monthly'

And if I have a slightly more complex URL structure like this one for posts (and who doesn’t have a posts model in their webapp):


Then I can do something a little more complex like this:

Post.find(:all).each do |post|
  sitemap.add show_path(post.named_route_generator), :lastmod => post.updated_at,
               :priority => 0.7, :changefreq => 'monthly'
  @comments = Comment.find( :all, :conditions => [ "post_id = ?", ] ).each do |comment|
    if !comment.blank?
      sitemap.add show_comment_path( comment.named_route_generator ), :lastmod => comment.updated_at,
               :priority => 0.4, :changefreq => 'monthly'

The only thing that I can’t seem to figure out if it supports is custom links in the URLs. Meaning links that are fairly convoluted and (for some reason) not generated with a link_to, but have a custom link generator. Other than that, you should probably go with sitemap_generator for your sitemap needs.

If you have a favorite Rails plugin for sitemaps, I would certainly like to hear about it. And if you aren’t using sitemaps and are worried about SEO, then shame on you.

Follow My Travels

Buy My Book


  • 2020
  • 2019
  • 2017
  • 2014
  • 2013
  • 2012
  • 2011
  • 2010
  • 2009
  • 2008
  • 2007
  • 2006

New Posts By Email