# Gemfile gem 'big_sitemap' # lib/tasks/sitemap.rake require 'big_sitemap' namespace :custom do desc "Generate sitemap" task :sitemap => :environment do include Rails.application.routes.url_helpers sitemap_options = { document_root: Rails.root.join('public'), url_options: { host: 'example.com' }, ping_google: true, ping_bing: true, gzip: true } if Rails.env.development? sitemap_options = { document_root: Rails.root.join('public'), url_options: { host: 'localhost', port: 3000 }, ping_google: false, ping_bing: false, gzip: false } end BigSitemap.generate(sitemap_options) do add root_path, change_frequency: 'daily', priority: 1.0 Shop.all.each do |shop| add shop_path(shop), change_frequency: 'daily', priority: 0.5 end end end end
然后使用命令执行
rake custom:sitemap
在public文件夹下产生sitemap.xml 和 sitemap_index.xml 两个文件。
可以把命令加在定时任务中,每天执行一次。
具体详情:https://github.com/alexrabarts/big_sitemap