SameShirtEveryDay.com

Personal blog of the one called Alex Gorbatchev, from Toronto, Canada.

Generate Google sitemap with Rails and RXML

Posted on May 26th, 2007 by Alex Gorbatchev. In Rails, Web Development. 4 comments!

It occurred to me that a good idea would be to add a Google sitemap to Noobkit, especially because it’s using frames. Here’s how I went about it:

In a regular controller I load up all my @packages but instead of a plain sitem_map.rhtml, I have a site_map.rxml file which looks like this:

xml.instruct!
xml.urlset "xmlns" => "http://www.google.com/schemas/sitemap/0.84" do
  xml.url do
    xml.loc         "http://test"
    xml.lastmod     w3c_date(Time.now)
    xml.changefreq  "monthly"
  end
  @packages.each do |package|
    xml.url do
      xml.loc         "http://www.noobkit.com/page/ref/#{package.full_name}"
      xml.lastmod     w3c_date(package.created_at)
      xml.changefreq  "monthly"
    end
  end
end

w3c_date goes into your_controller_helper.rb.

def w3c_date(date)
  date.utc.strftime("%Y-%m-%dT%H:%M:%S+00:00")
end

That’s all to it. Packages provide entry points to their entire API trees and should be enough information for Google to start crawling there.

You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

4 comments.

Leave a Reply

Allowed tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> , rel="nofollow" in use - no link dropping, no keywords or domains as names; do not spam, and do not advertise!

home
Subscribe to this blog Follow me on Twitter My bookmarks on Delicious My photography on Flickr