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)
    render_without_layout
  end

end
    

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.instruct!
xml.urlset(:xmlns=>'http://www.google.com/schemas/sitemap/0.84') {
  for page in @pages
    xml.url {
      xml.loc(url_for :controller=>'page',
                      :action => 'show',
                      :id => page.id,
                      :trailing_slash => true,
                      :only_path => false)
      xml.lastmod(page.updated_on.strftime('%Y-%m-%d'))
    }
  end
}
    

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="http://www.google.com/schemas/sitemap/0.84">
  <url>
    <loc>http://localhost:3000/browse/welcome/</loc>
    <lastmod>2005-08-23</lastmod>
  </url>
  <url>
    <loc>http://localhost:3000/browse/contact/</loc>

    <lastmod>2005-08-23</lastmod>
  </url>
  <url>
    <loc>http://localhost:3000/browse/links/</loc>
    <lastmod>2005-08-23</lastmod>
  </url>
  <url>

    <loc>http://localhost:3000/browse/about/</loc>
    <lastmod>2005-08-23</lastmod>
  </url>
</urlset>
    

Setting up the Route

Google “strongly recommends” that you place the sitemap in the root directory. Eg: http://www.example.com/sitemap.xml

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.

Update

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:
http://www.google.com/webmasters/sitemaps/docs/en/overview.html

For information about Rails and XML have a look at:
http://api.rubyonrails.com/classes/Builder/XmlMarkup.html