【Rails】使用Gem生成sitemap

安装Gem

1
gem 'sitemap_generator'

生成config/sitemap.rb

1
bundle exec rake sitemap:install

修改SitemapGenerator::Sitemap.default_host

1
SitemapGenerator::Sitemap.default_host = 'http://example.com'

在public/下生成sitemap文件

1
2
3
4
5
# Development/Test环境
bundle exec rake sitemap:refresh:no_ping
# Production环境
bundle exec rake sitemap:refresh

结果如下:

1
2
3
4
5
rake sitemap:refresh:no_ping
> In /app_path/to/public/
> + sitemap1.xml.gz 1 links > / 340 Bytes
> + sitemap_index.xml.gz 1 sitemaps > / 241 Bytes
> Sitemap stats: 1 links / 1 sitemaps / 0m00s

修改Robots.txt

1
Sitemap: http://www.example.com/sitemap.xml.gz

Deployments & Capistrano

1
require 'capistrano/sitemap_generator'

可使用的命令:

1
2
3
deploy:sitemap:create #Create sitemaps without pinging search engines
deploy:sitemap:refresh #Create sitemaps and ping search engines
deploy:sitemap:clean #Clean up sitemaps in the sitemap path

参考网站

http://morizyun.github.io/blog/meta-tags-sitemap-generator-rails-seo/

https://github.com/kjvarga/sitemap_generator

Yunjie Zhang wechat
扫一扫上面的二维码加我微信