Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
This plugin will generate a sitemap.xml from sitemap.rb whose format is very similar to routes.rb
Ruby
branch: master

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
lib fix lastmod format
spec add lashmod by updated_at
tasks make ping task independent on generate task
MIT-LICENSE first commit, can generate sitemap.xml from sitemap.rb
README.textile better README
Rakefile remove test directory
init.rb better layout, require depencies and fix spec issue
install.rb
uninstall.rb first commit, can generate sitemap.xml from sitemap.rb

README.textile

Sitemap

This plugin will generate a sitemap.xml or sitemap.xml.gz from sitemap.rb whose format is very similar to routes.rb, and ping search engine to update sitemap.xml or sitemap.xml.gz.
It supports resources, namespace, root, connect, named_route and nested routes like routes.rb and add some additional keyword substitution and priority for sitemap.


Install

  • install dependency gem builder
    sudo gem install builder
  • install sitemap plugin
    script/plugin install git://github.com/flyerhzm/sitemap.git

Example

  • Create config/sitemap.rb file, or you can copy config/routes.rb to config/sitemap.rb
At first, you should define host (required) and default priority (optional) in config/sitemap.rb for sitemap.

Sitemap::Routes.host = 'http://www.sitemap.com'
Sitemap::Routes.priority = 0.8    # default is 1.0
Then, you can define the routes for sitemap.

Sitemap::Routes.draw do |map|
  # resources routes
  map.resources :posts, :collection => {:all => :get}, :member => {:display => :get}, :has_many => :comments

  # root routes
  map.root :controller => 'posts', :action => 'index'

  # namespace routes
  map.namespace(:admin) do |admin|
    admin.resources :posts
  end

  # connect routes which need additional keyword :substitution, 
  # this is a hash, one of the key/value pairs define model name, 
  # the others define which attribute value of the model replace the connect keyword. 
  # As the route defines below, the url will be replaced with "posts/#{post.year}/#{post.month}/#{post.day}", the post the object of Post
  map.connect 'posts/:year/:month/:day', :controller => 'posts', :action => 'find_by_date', :substitution => {:model => 'Post', :year => 'year', :month => 'month', :day => 'day'}

  # connect routes, you can also use substitution for an array substitution
  map.connect 'posts/:locale', :controller => 'posts', :action => 'index', :substitution => {:locale => ['en', 'zh', 'fr']}

  # named_route routes, these routes should use :substitution keyword if necessary
  map.sitemap '/sitemap', :controller => 'sitemaps', :action => 'index'

  # nested routes
  map.resources :categories, :except => ['index', 'show'] do |category|
    category.resources :posts, :except => 'show'
  end

  # define priority for routes, this can be applied to every route
  map.sitemap '/sitemap', :controller => 'sitemaps', :action => 'index', :priority => 0.8
  map.root :controller => 'posts', :action => 'index', :priority => 0.5

  # define changefreq for routes, this can be applied to every route
  map.sitemap '/sitemap', :controller => 'sitemaps', :action => 'index', :changefreq => Sitemap::ChangeFreq::ALWAYS
  map.root :controller => 'posts', :action => 'index', :changefreq => Sitemap::ChangeFreq::MONTHLY
end

Important: substitution keyword can only define one model, if you need replace attributes in two models or more, I recommend you define a virtual attribute, or use delegate.

Important: You can check the spec/sitemap_spec.rb to see the use of Sitemap::Routes.

  • run rake task.
rake sitemap:generate or rake sitemap:generate FORMAT=gzip, the sitemap.xml or sitemap.xml.gz (version 0.9) will be generated under public directory. rake sitemap:ping or rake sitemap:ping SEARCH_ENGINE=google,bing FORMAT=gzip, tell search engine to update the sitemap.xml or sitemap.xml.gz, now support google, bing, yahoo and ask. The generated sitemap.xml is as follows:

<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
  <url>
    <loc>http://www.sitemap.com</loc>
    <lastmod>2009-08-09</lastmod>
    <priority>0.5</priority>
    <changefreq>monthly</changefreq>
  </url>
  <url>
    <loc>http://www.sitemap.com/sitemap</loc>
    <lastmod>2009-08-09</lastmod>
    <priority>0.8</priority>
    <changefreq>always</changefreq>
  </url>
  <url>
    <loc>http://www.sitemap.com/posts</loc>
    <lastmod>2009-08-09</lastmod>
    <priority>1.0</priority>
  </url>
  <url>
    <loc>http://www.sitemap.com/posts/1</loc>
    <lastmod>2009-08-09</lastmod>
    <priority>1.0</priority>
  </url>
  <url>
    <loc>http://www.sitemap.com/posts/2</loc>
    <lastmod>2009-08-09</lastmod>
    <priority>1.0</priority>
  </url>
  <url>
    <loc>http://www.sitemap.com/posts/all</loc>
    <lastmod>2009-08-09</lastmod>
    <priority>1.0</priority>
  </url>
  <url>
    <loc>http://www.sitemap.com/posts/1/display</loc>
    <lastmod>2009-08-09</lastmod>
    <priority>1.0</priority>
  </url>
  <url>
    <loc>http://www.sitemap.com/posts/2/display</loc>
    <lastmod>2009-08-09</lastmod>
    <priority>1.0</priority>
  </url>
  <url>
    <loc>http://www.sitemap.com/admin/posts</loc>
    <lastmod>2009-08-09</lastmod>
    <priority>1.0</priority>
  </url>
  <url>
    <loc>http://www.sitemap.com/admin/posts/1</loc>
    <lastmod>2009-08-09</lastmod>
    <priority>1.0</priority>
  </url>
  <url>
    <loc>http://www.sitemap.com/admin/posts/2</loc>
    <lastmod>2009-08-09</lastmod>
    <priority>1.0</priority>
  </url>
  <url>
    <loc>http://www.sitemap.com/posts/2009/8/9</loc>
    <lastmod>2009-08-09</lastmod>
    <priority>1.0</priority>
  </url>
  <url>
    <loc>http://www.sitemap.com/posts/2009/8/10</loc>
    <lastmod>2009-08-09</lastmod>
    <priority>1.0</priority>
  </url>
  <url>
    <loc>http://www.sitemap.com/categories/1/posts</loc>
    <lastmod>2009-08-09</lastmod>
    <priority>1.0</priority>
  </url>
  <url>
    <loc>http://www.sitemap.com/categories/2/posts</loc>
    <lastmod>2009-08-09</lastmod>
    <priority>1.0</priority>
  </url>
</urlset>

Copyright © 2009 Richard Huang (flyerhzm@gmail.com), released under the MIT license

Something went wrong with that request. Please try again.