typoをGoogle Sitemapに対応させる

Posted by elanbeat Sun, 14 May 2006 17:30:32 GMT

Googleのインデックスに自サイトのURLを効率的に登録する方法としてGoogle Sitemap が利用できます。typoでもGoogle Sitemapを利用できるように、Google Sitemap Protocol を自動で生成させてみます。

準備

Google Sitemapについてはここでは説明しません。すでにアカウントを持っていて、操作について知っているものとします。例えば、実際の利用には自サイトの所有者の確認等の作業が必要となります。

方針

typoでsitemap.xmlを生成する方法は、

で解説されています。またこの元ネタとして、Ruby on Railsのフレームワークでsitemap.xmlを生成する基本的な方法が、

で解説されています。

前者の様にtypoのxml_controller.rbで生成してももちろんよいのですが、xml_controller.rbにパッチが当たることがある1ため、ここでは後者の方法でcontrollerを独立させてることにします。

TypoでのSitemap生成

controllerの作成

app/controllers/sitemap_controller.rbを作成する。


class SitemapController < ContentController
  caches_action :sitemap
  session :off

  def sitemap
    @headers[‘Content-Type’] = ‘text/xml; charset=utf-8’
    @articles = Article.find(:all, :conditions => ‘published=1’, :order => ‘created_at DESC’)
    @pages = Page.find(:all, :order => ‘created_at DESC’)
  end
end

注意: 最新のchangeset(1042~1053あたり)ではpublish関連で大幅な変更がなされています。最新版ではcreated_atの代わりにpublished_atを使うべきですが、ここではcreated_atを使っています。

viewの作成

app/views/sitemap/sitemap.rxmlを作成する。


xml.instruct! :xml, :version=> ‘1.0’, :encoding => ‘UTF-8’
xml.urlset( :xmlns => ‘http://www.google.com/schemas/sitemap/0.84’) do
  # First entry is the main entry to the site
  xml.url do
    xml.loc server_url_for(:controller => “articles”)
    xml.changefreq ‘daily’
    xml.priority ‘0.9’
  end

  for entry in @articles
    xml.url do
      xml.loc article_url(entry, false)
      xml.lastmod entry.updated_at.xmlschema
      xml.changefreq ‘weekly’
      # xml.priority ‘0.5’ # default
    end
  end

  for page in @pages
    xml.url do
      xml.loc url_for(:controller => “pages”, :action => page.name, :only_path => false )
      xml.lastmod page.updated_at.xmlschema
      xml.changefreq ‘weekly’
      xml.priority ‘0.5’
    end
  end
end

注意: ここでは元記事 に従って、indexの優先度を0.9に、各記事を0.5にしています。

route.rbの設定

config/route.rbにマッピングを追加する。


  map.xml ‘sitemap.xml’, :controller => ‘sitemap’, :action => ‘sitemap’

テーマ毎に設定する

以上の設定でhttp://yourblog/sitemap.xmlが生成されるようになったはずです。複数のブログを運営しているなどでtheme毎に生成の仕方を変化させたい場合は、app/views/sitemap/sitemap.rxmlをtheme/your_theme/views/sitemap/sitemap.rxmlにコピーして変更します。

パラメータのチューニング

上記ではindexのプライオリティを0.9, 各記事のプライオリティを0.5(デフォルト)にしてありますが、私はindexを0.5, 各記事を0.9にしています。本ブログでのindexは所詮記事の冒頭部分のリストでしかありませんし、またタイムラグも発生するためです。

また、今回はアーカイブやタグ関連のURLについては生成していませんが、様子を見ながら追加していくことも必要かもしれません。本ブログの場合はindexと同様にプライオリティを下げる方向にする予定です。(sitemap.xmlにURLがなくても通常のクロールによりインデックス化されます。詳細はGoogle Sitemapのヘルプをご覧下さい。)

その他、更新頻度やクロールさせるページ毎のチューニングは、サイトのポリシーに従って設定する必要があります。

参考

1 最近ではAtom Feed関連の修正など

広告

Posted in ,  | Tags , , , ,  | no comments | no trackbacks

Comments

Trackbacks

Use the following link to trackback from your own site:
/articles/trackback/205

(leave url/email »)

   Preview comment