From Google’s website

“If your site has dynamic content or pages that aren’t easily discovered by following links, you can use a Sitemap file to provide information about the pages on your site. This helps the spiders know what URLs are available on your site and about how often they change.”

I thought it would be a good thing to have Google Sitemaps integrated into some of my Ruby on Rails projects. Because Ruby on Rails has excellent XML support, I went ahead and implemented Google Sitemaps into one of my latest projects.

The Controller

Nearly every site I’ve made so far in Rails has a page controller. So in this example I will use a page controller with a sitemap method inside it.

class PageController < ApplicationController

  def sitemap
    @pages = Page.find(:all)


As you can see in my example controller, the Rails application has a page model. You can replace this with as many models as you wish. For example, if you also had news on your application, you might want to add @news= News.find(:all) to the controller and change the view as required.

The View (sitemap.rxml)

xml.urlset(:xmlns=>'') {
  for page in @pages
    xml.url {
      xml.loc(url_for :controller=>'page',
                      :action => 'show',
                      :id =>,
                      :trailing_slash => true,
                      :only_path => false)

The RXML is extremely straight-foward as you can see.

With the above setup the output looks like this…

<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="">



Setting up the Route

Google “strongly recommends” that you place the sitemap in the root directory. Eg:

To do this we need to setup a route. Open up config/routes.rb and add…

map.connect 'sitemap.xml', :controller => 'page', :action => 'sitemap' 

...near the top of the file. Now when we request http://localhost:3000/sitemap.xml the sitemap will appear.

Submit your Sitemap to Google

Now you have setup the sitemap you will need to submit it to Google.


Mark extends this example by implementing automatic submissions to Google when your sitemap changes.

More Information

For more information about Google’s Sitemaps take a look at the BETA website:

For information about Rails and XML have a look at: