Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
171 lines (117 sloc) 4.33 KB

Rails Sitemap Generator

The Rails Sitemap Generator plugin makes it very easy to generate sitemaps for your Rails app.

In addition to simplifying sitemap creation the plugin also includes the following features:

  • Sitemap validation. A sitemap can contain about 50 000 items and be no more than x MB.
  • Search engine ping. The plugin notifies search engines each time your sitemap is updated.


Installation is done with the script/plugin script:

  $ script/plugin install  git://

After installing the plugin you need to:

  • Configure the plugin (config/sitemap.yml)
  • Include a call to the sitemap method from each model you want to include in the sitemap

Configuring the plugin

A configuration file (config/sitemap.yml) is automatically created by the installation script.

Below is a sample configuration file, which also contains the default values for the models:


  limit: 5000
  priority: 1
  change_frequency: weekly
  ping: true

The options:

  • domain: before using the sitemap generator you need to tell the plugin the domain where your application is deployed. This is because the sitemap must contain the full URL, not just the URI, of all resources.
  • limit: specifies how many model instances you want to include in the sitemap
  • priority: specifies the priority of the model
  • change_frequency: specifies how often the data changes: always, hourly, daily, weekly, monthly, yearly or never
  • ping: specifies whether the plugin should ping search engines or not

You can find a more comprehensive description of these concepts on the Wikipedia page on sitemaps

Configuring ActiveRecord models

Each model you want included in the sitemap must call the sitemap(options) method.

  class Post < ActiveRecord::Base

The default options for each model are taken from the configuration file (config/sitemap.yml). You’ll most likely want each model to have a different priority and change_frequency. The plugin allows you to override the default options as shown in this example:

  class Post < ActiveRecord::Base
    sitemap :change_frequency => :weekly, :limit => 1000, :priority => 0.5


The plugin will automatically order the data using the most appropriate Rails magic column, which is the first of the following columns it can find: updated_at, updated_on, created_at or created_on.

You can override this behavior when calling the sitemap method:

  class Post < ActiveRecord::Base
    sitemap :order => 'points DESC'

Custom sitemaps

Custom sitemaps can be generated by passing a block to the sitemap method. A builder instance representing the sitemap is passed to this block. Inside the block you can do whatever is needed:


  sitemap do |xml|

    Post.all.each do |place|
      xml.url do
        xml.loc "http://#{SitemapGenerator::Options.domain}/#{post.to_param}'"

        xml.lastmod SitemapGenerator::Helpers.instance.w3c_date(post.updated_at)
        xml.changefreq 'weekly'
        xml.priority '1'




After configuring the plugin you can generate the sitemap with rake:

  $ rake sitemap:generate

You can also generate the sitemap programatically like this:

Scheduled updates with cron

Open the cron configuration with the following command:

$ sudo crontab -e

Add the following:


# m h  dom mon dow   command

# Update the sitemap every day at 24:00

00 00 * * * cd /var/www/xxx/current && RAILS_ENV=production rake sitemap:generate



Christian Hellsten (Aktagon Ltd.)


Dan Hawkins