elanbeat's wiki
TypoGoogleSitemapHowTo

TypoをGoogle Sitemapに対応させる

準備

Google Sitemap1, (日本語版2)のアカウントを作成し、サイトの認証を済ませておくこと。

TypoでのSitemap生成

xml_controller.rbにactionを追加する方法3でもよいが、ここでは専用のcontollerを作成する方法4をとる。

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

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

route.rbの設定


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

テーマ毎に設定する

theme/your_theme/views/sitemap/sitemap.rxmlにコピーして変更する。

参考

1 Google Sitemap [google.com]

2 Google Sitemap – ようこそ! [google.co.jp]

3 IP Angels :: Generating Google SiteMaps with Ruby on Rails [ipangels.com]

4 Google Sitemaps in Typo [hedleyrobertson.com]