I recently added a dynamic sitemap to my blogging application. These steps will help you get started.
Add this route to the bottom of your config/routes.rbfile (more specific routes should be listed above):
map.sitemap '/sitemap.xml', :controller => 'sitemap'
Create SitemapController(application / 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 }
end
end
end
end
&mdash. , , Post.
(app/views/sitemap/index.xml.builder):
base_url = "http://#{request.host_with_port}"
xml.instruct! :xml, :version=>'1.0'
xml.tag! 'urlset', 'xmlns' => 'http://www.sitemaps.org/schemas/sitemap/0.9' do
for post in @posts do
xml.tag! 'url' do
xml.tag! 'loc', "#{base_url}#{post.permalink}"
xml.tag! 'lastmod', post.last_modified
xml.tag! 'changefreq', 'monthly'
xml.tag! 'priority', '0.5'
end
end
end
! , http://localhost:3000/sitemap.xml ( Mongrel) , , cURL.
, stale? HTTP 304 Not Modified, , Sitemap.