Skip to content
Find file
Fetching contributors…
Cannot retrieve contributors at this time
135 lines (87 sloc) 5.83 KB


This plugin enables 'enterprise-class' Google Sitemaps to be easily generated for a Rails site as a rake task, using a simple 'Rails Routes'-like DSL.

Raison d'être

Most of the Sitemap plugins out there seem to try to recreate the Sitemap links by iterating the Rails routes. In some cases this is possible, but for a great deal of cases it isn't.

a) There are probably quite a few routes in your routes file that don't need inclusion in the Sitemap. (AJAX routes I'm looking at you.)


b) How would you infer the correct series of links for the following route?

map.zipcode 'location/:state/:city/:zipcode', :controller => 'zipcode', :action => 'index'

Don't tell me it's trivial, because it isn't. It just looks trivial.

So my idea is to have another file similar to 'routes.rb' called 'sitemap.rb', where you can define what goes into the Sitemap.

Here's my solution:

Zipcode.find(:all, :include => :city).each do |z|
  sitemap.add zipcode_path(:state =>, :city =>, :zipcode => z)

Easy hey?

Other Sitemap settings for the link, like lastmod, priority, changefreq and host are entered automatically, although you can override them if you need to.

Other "difficult" Sitemap issues, solved by this plugin:

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


  1. Install plugin as normal

    ./script/plugin install git://

  2. Installation should create a 'config/sitemap.rb' file which will contain your logic for generation of the Sitemap files. (If you want to recreate this file manually run rake sitemap:install)

  3. Run rake sitemap:refresh as needed to create Sitemap files. This will also ping all the 'major' search engines.

    Sitemaps with many urls (100,000+) take quite a long time to generate, so if you need to refresh your Sitemaps regularly you can set the rake task up as a cron job.

  4. Finally, and optionally, add the following to your robots.txt file.

    Sitemap: <hostname>/sitemap_index.xml.gz

    The robots.txt Sitemap URL should be the complete URL to the Sitemap Index, such as:

Example 'config/sitemap.rb'

# Set the host name for URL creation
SitemapGenerator::Sitemap.default_host = ""

SitemapGenerator::Sitemap.add_links do |sitemap|
  # Put links creation logic here.
  # The Root Path ('/') and Sitemap Index file are added automatically.
  # Links are added to the Sitemap output in the order they are specified.
  # Usage: sitemap.add path, options
  #        (default options are used if you don't specify them)
  # Defaults: :priority => 0.5, :changefreq => 'weekly', 
  #           :lastmod =>, :host => default_host

  # Examples:

  # add '/articles'
  sitemap.add articles_path, :priority => 0.7, :changefreq => 'daily'

  # add all individual articles
  Article.find(:all).each do |a|
    sitemap.add article_path(a), :lastmod => a.updated_at

  # add merchant path
  sitemap.add '/purchase', :priority => 0.7, :host => ""



1) Only tested/working on Rails 2.3.2, no guarantees made for any other versions of Rails.

2) For large sitemaps it may be useful to split your generation into batches to avoid running out of memory. E.g.:

# add movies
Movie.find_in_batches(:batch_size => 1000) do |movies|
  movies.each do |movie|
    sitemap.add "/movies/show/#{movie.to_param}", :lastmod => movie.updated_at, :changefreq => 'weekly'

Known Bugs

  • states that no Sitemap XML file should be more than 10Mb uncompressed. The plugin does not check this.
  • Currently only supports one Sitemap Index file, which can contain 50,000 Sitemap files which can each contain 50,000 urls, so it only supports up to 2,500,000,000 (2.5 billion) urls. I personally have no need of support for more urls, but plugin could be improved to support this.


I'd love to see some statistics on usage for this plugin... I.e. Approximately how many links per site are being indexed and how long it takes to generate etc.

If you don't mind sharing this kind of information, put your name and site on the SitemapGenerator Usage page.

Oh wait, you mean money?

... Buy me a Boost Juice when next we meet.

Copyright (c) 2009 Adam @, released under the MIT license

Something went wrong with that request. Please try again.