2014年10月31日金曜日

Railsでsitemap_generator使ってS3にサイトマップを作成する方法

Railsでサイトマップを作成して、登録する方法です。
サイトマップの生成は、GitHub - kjvarga/sitemap_generatorのGemを利用して行います。
また、サイトマップ自体はアマゾンのS3に保存しておきます。

インストール

Gemfileに下記を追加して、bundle install

gem 'sitemap_generator'

設定ファイルの作成

下記を実行して、config/sitemap.rb を作成します。

rake sitemap:install

サンプルコード

本来であれば、上記で生成されたconfig/sitemap.rbに設定を書けばよいと思いますが、
rakeでサイトマップ生成とS3のアップロードを行うようにしました。

コードは以下のような感じです。

  namespace :batch do
    desc 'サイトマップを作成して、S3にアップする。'
    task :create_site_map => :environment do |task|
      success_msgs = []
      success_msgs << 'success'

      batch_start_at = Time.now
      puts "run #{task.name} with env: #{Rails.env} : #{batch_start_at}"

      begin

        SitemapGenerator::Sitemap.default_host = 'http://ore-tech.blogspot.jp/'
        SitemapGenerator::Sitemap.public_path = 'tmp/'

        SitemapGenerator::Sitemap.adapter = SitemapGenerator::S3Adapter.new({
            :aws_access_key_id    => 'xxxxxxxxxxxxxxxxxxxxxxxxx',
            :aws_secret_access_key => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
            :aws_region            => 'ap-xxxxxxx-1', # リージョン 東京の場合、ap-northeast-1
            :fog_provider          => 'AWS',
            :fog_directory         => 'bucket-name' # bucket name
        })
        SitemapGenerator::Sitemap.sitemaps_host = 'http://bucket-name.s3-ap-xxxxxxx-1.amazonaws.com'

        site_maps_path = 'sitemap/'
        unless Rails.env == 'production'
          site_maps_path = "sitemap/#{Rails.env}/"
        end

        SitemapGenerator::Sitemap.sitemaps_path = site_maps_path

        SitemapGenerator::Sitemap.create do

          # home
          add '/home', :changefreq => 'daily', :priority => 0.9

          # contact 
          add '/contact_us', :changefreq => 'weekly'

          # Blog
          group(:sitemaps_path => site_maps_path, :filename => 'blog') do
            Blog.all.each do |blog|
              add blog.url, lastmod: blog.updated_at
            end
          end

        end

        if Rails.env == 'production'
          SitemapGenerator::Sitemap.ping_search_engines
        end

        batch_end_at = Time.now
        puts "end time : #{batch_end_at}"
        puts "Processing time #{batch_end_at - batch_start_at}"

      rescue => e
        puts "error occurred batch : #{Time.now}"
        puts "error occurred batch : #{e.message}"
      end

    end
  end

サイトマップ生成の設定

サイトマップ生成の設定は、主に下記の部分で行っています。

SitemapGenerator::Sitemap.default_host = 'http://ore-tech.blogspot.jp/'
SitemapGenerator::Sitemap.public_path = 'tmp/'

SitemapGenerator::Sitemap.adapter = SitemapGenerator::S3Adapter.new({
    :aws_access_key_id    => 'xxxxxxxxxxxxxxxxxxxxxxxxx',
    :aws_secret_access_key => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
    :aws_region            => 'ap-xxxxxxx-1', # リージョン 東京の場合、ap-northeast-1
    :fog_provider          => 'AWS',
    :fog_directory         => 'bucket-name' # bucket name
})
SitemapGenerator::Sitemap.sitemaps_host = 'http://bucket-name.s3-ap-xxxxxxx-1.amazonaws.com'

site_maps_path = 'sitemap/'
unless Rails.env == 'production'
  site_maps_path = "sitemap/#{Rails.env}/"
end

SitemapGenerator::Sitemap.sitemaps_path = site_maps_path

上記設定を行った場合、下記の場所にサイトマップ用のファイルが生成されます。

application側

/tmp/sitemap/blog.xml.gz
/tmp/sitemap/sitemap.xml.gz
/tmp/sitemap/sitemap1.xml.gz

S3側

/bucket-name/sitemap/blog.xml.gz
/bucket-name/sitemap/sitemap.xml.gz
/bucket-name/sitemap/sitemap1.xml.gz

また、下記を行っているので実行されるenv環境によって、生成されるファイルの場所が異なります。

site_maps_path = 'sitemap/'
unless Rails.env == 'production'
  site_maps_path = "sitemap/#{Rails.env}/"
end

production => /bucket-name/sitemap/
staging => /bucket-name/sitemap/staging/blog.xml.gz
development => /bucket-name/sitemap/development/blog.xml.gz

サイトの登録

主には以下の部分で、ページを登録しています。
add メソッドでページ単位で登録していきます。

SitemapGenerator::Sitemap.create do

   # home
   add '/home', :changefreq => 'daily', :priority => 0.9

   # contact 
   add '/contact_us', :changefreq => 'weekly'

   # Blog
   group(:sitemaps_path => site_maps_path, :filename => 'blog') do
     Blog.all.each do |blog|
       add blog.url, :lastmod => blog.updated_at
     end
   end

 end

addメソッドのオプション

:changefreq

ページの予想される更新頻度
デフォルトは、weekly

設定可能な値は以下の通り

  • always:アクセスがあるたびに内容を更新するページに使用します。
  • hourly
  • daily
  • weekly
  • monthly
  • yearly
  • never: アーカイブ ページの URL に使用します。

:priority

サイト内のURLの優先順位を指定します。
0.1(重要でない)~1.0(重要)の範囲で指定します。
デフォルトは 0.5

ここの数値で、掲載順位には影響はないようです。

:lastmod

最終更新日を指定します
デフォルトは Time.now 現在日時

:host

ホストを指定します。
デフォルトはdefault_hostの値。
loginページなど、https://xxxxxなどに変更したい時などに使えると思います。

:expires

有効期限を設定可能です。

group

groupを使うことで、別ファイルで管理することができます。

サイトマップの登録

routes.rb

Googleのウェブマスターツールに /sitemapと登録させるために
routes.rbでS3のサイトマップのパスへリダイレクトさせます。

 match '/sitemap'  =>  redirect('http://backet-name.s3.amazonaws.com/sitemap/sitemap.xml.gz')

robots.txt

検索エンジンのrobot用に以下を追加

Sitemap: http://backet-name.s3.amazonaws.com/sitemap/sitemap.xml.gz

参考サイト

Goolge サイトマップを作成する

0 件のコメント:

コメントを投稿

statistics

Arsip