Skip to content

Instantly share code, notes, and snippets.

@harish86
Last active December 17, 2015 07:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save harish86/5574792 to your computer and use it in GitHub Desktop.
Save harish86/5574792 to your computer and use it in GitHub Desktop.
Building xml sitemaps in rails.
# Below is a snippet to generate sitemap.xml file
# Reference: http://www.sitemaps.org, http://en.wikipedia.org/wiki/Site_map, http://support.google.com/webmasters/bin/answer.py?hl=en&answer=183668&topic=8476&ctx=topic
xml.instruct!
xml.urlset(:xmlns => "http://www.sitemaps.org/schemas/sitemap/0.9") {
@items.each do |item|
xml.url {
xml.loc(item_url(item))
xml.changefreq("daily")
xml.priority("0.5")
}
end
}
# Below is a snippet to generate sitemap.xml file
# Reference: http://www.sitemaps.org, http://en.wikipedia.org/wiki/Site_map, http://support.google.com/webmasters/bin/answer.py?hl=en&answer=183668&topic=8476&ctx=topic
xml.instruct!
xml.urlset(:xmlns => "http://www.sitemaps.org/schemas/sitemap/0.9") {
@resources.each do |resource|
xml.url {
xml.loc(resource_url(resource))
xml.changefreq("weekly")
xml.priority("0.5")
}
end
}
get "/sitemapindex.xml" => "welcome#sitemapindex", :as => :sitemapindex
resoureces :resources do
collection do
get :sitemap
end
end
resoureces :items do
collection do
get :sitemap
end
end
# Below is a snippet to generate sitemapindex.xml file. Sitemapindex file is required to list multiple sitemap files. Useful when the site has multiple sitemap files
# Reference: http://www.sitemaps.org, http://en.wikipedia.org/wiki/Sitemap_index, http://support.google.com/webmasters/bin/answer.py?hl=en&answer=71453
xml.instruct!
xml.sitemapindex(:xmlns => "http://www.sitemaps.org/schemas/sitemap/0.9") {
xml.sitemap {
xml.loc(sitemap_resources_url(:format => :xml))
xml.lastmod(Time.zone.now.strftime("%Y-%m-%d"))
}
xml.sitemap {
xml.loc(sitemap_items_url(:format => :xml))
xml.lastmod(Time.zone.now.strftime("%Y-%m-%d"))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment