Google sitemap files for Rails projects

Ruby on-RailsSitemapGoogle Search-Console

Ruby on-Rails Problem Overview


Is there an easy way to create a sitemaps file for Rails projects? Especially for dynamic sites (such as Stack Overflow for example) there should be a way to dynamically create a sitemaps file. What is the way to go in Ruby and/or Rails?

What would you suggest? Is there any good gem out there?

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

Add this route towards the bottom of your config/routes.rb file (more specific routes should be listed above it):

map.sitemap '/sitemap.xml', :controller => 'sitemap'

Create the SitemapController (app/controllers/sitemap_controller):

class SitemapController < ApplicationController
  layout nil

  def index
    headers['Content-Type'] = 'application/xml'
    last_post = Post.last
    if stale?(:etag => last_post, :last_modified => last_post.updated_at.utc)
      respond_to do |format|
        format.xml { @posts = Post.sitemap } # sitemap is a named scope
      end
    end
  end
end

—As you can see, this is for a blog, so is using a Post model. This is the HAML view template (app/views/sitemap/index.xml.haml):

- base_url = "http://#{request.host_with_port}"
!!! XML
%urlset{:xmlns => "http://www.sitemaps.org/schemas/sitemap/0.9"}
  - for post in @posts
    %url
      %loc #{base_url}#{post.permalink}
      %lastmod=post.last_modified
      %changefreq monthly
      %priority 0.5

That's it! You can test it by bringing up http://localhost:3000/sitemap.xml (if using Mongrel) in a browser, or perhaps by using cURL.

Note that the controller uses the stale? method to issue a HTTP 304 Not Modified response if there are no new posts sinces the sitemap was last requested.

Solution 2 - Ruby on-Rails

Now for rails3, it is better off using full-featured sitemap_generator gem.

Solution 3 - Ruby on-Rails

I love John Topley's answer because it is so simple and elegant, without the need for a gem. But it's a bit dated, so I've updated his answer for Rails 4 and Google Webmaster Tools' latest sitemap guidelines.

config/routes.rb:

get 'sitemap.xml', :to => 'sitemap#index', :defaults => { :format => 'xml' }

app/controllers/sitemap_controller.rb:

class SitemapController < ApplicationController
  layout nil

  def index
    headers['Content-Type'] = 'application/xml'
    last_post = Post.last
    if stale?(:etag => last_post, :last_modified => last_post.updated_at.utc)
      respond_to do |format|
        format.xml { @posts = Post.all }
      end
    end
  end
end

app/views/sitemap/index.xml.haml:

!!! XML
%urlset{:xmlns => "http://www.sitemaps.org/schemas/sitemap/0.9"}
  - for post in @posts
    %url
      %loc #{post_url(post)}/
      %lastmod=post.updated_at.strftime('%Y-%m-%d')
      %changefreq monthly
      %priority 0.5

You can test it by bringing up localhost:3000/sitemap.xml.

Solution 4 - Ruby on-Rails

I would recommend that you check out the sitemap_generator gem. It handles all of these issues for you...and really, who wants to mess around authoring XML?

Here is an example sitemap to show how you use your Rails models and path helpers to generate your sitemap URLs:

# config/sitemap.rb
SitemapGenerator::Sitemap.default_host = "http://www.example.com"
SitemapGenerator::Sitemap.create do
  add '/contact_us'
  Content.find_each do |content|
    add content_path(content), :lastmod => content.updated_at
  end
end

Then you use Rake tasks to refresh as often as you would like. It really is that simple :)

Solution 5 - Ruby on-Rails

Here is a plugin for creating sitemaps in Ruby on Rails: Ruby on Rails sitemap plugin. It takes care of most of the sitemap logic and generation. The plugin is from my homepage.

Example configuration:

Sitemap::Map.draw do

  # default page size is 50.000 which is the specified maximum at http://sitemaps.org.
  per_page 10

  url root_url, :last_mod => DateTime.now, :change_freq => 'daily', :priority => 1

  new_page!

  Product.all.each do |product|
    url product_url(product), :last_mod => product.updated_at, :change_freq => 'monthly', :priority => 0.8
  end

  new_page!

  autogenerate  :products, :categories,
                :last_mod => :updated_at,
                :change_freq => 'monthly',
                :priority => 0.8

  new_page!

  autogenerate  :users,
                :last_mod => :updated_at,
                :change_freq => lambda { |user| user.very_active? ? 'weekly' : 'monthly' },
                :priority => 0.5

end

Best regards, Lasse

Solution 6 - Ruby on-Rails

This article explains how a sitemap can be generated.

Basically should should create a controller which finds all pages (eg your Posts) and put in into an XML file. Next you tell Google about the location of the XML file and when your website is updated.

A simple Google rails sitemap query reveals lots of other articles explaining basically the same thing.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionz3ckoView Question on Stackoverflow
Solution 1 - Ruby on-RailsJohn TopleyView Answer on Stackoverflow
Solution 2 - Ruby on-RailsNinadView Answer on Stackoverflow
Solution 3 - Ruby on-RailsAaron GrayView Answer on Stackoverflow
Solution 4 - Ruby on-RailsKarl VargaView Answer on Stackoverflow
Solution 5 - Ruby on-RailsLasse BunkView Answer on Stackoverflow
Solution 6 - Ruby on-RailsVegerView Answer on Stackoverflow