Rails项目的Google站点地图文件

z3c*_*cko 59 sitemap ruby-on-rails google-webmaster-tools

有没有一种简单的方法来为Rails项目创建站点地图文件?特别是对于动态站点(例如Stack Overflow),应该有一种动态创建站点地图文件的方法.Ruby和/或Rails的方法是什么?

你会建议什么?那里有什么好的宝石吗?

Joh*_*ley 47

将此路线添加到config/routes.rb文件底部(应在其上方列出更具体的路线):

map.sitemap '/sitemap.xml', :controller => 'sitemap'
Run Code Online (Sandbox Code Playgroud)

创建SitemapController(app/controllers/sitemap_controller):

class SitemapController < ApplicationController
  layout nil

  def index
    headers['Content-Type'] = 'application/xml'
    last_post = Post.last
    if stale?(:etag => last_post, :last_modified => last_post.updated_at.utc)
      respond_to do |format|
        format.xml { @posts = Post.sitemap } # sitemap is a named scope
      end
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

- 正如您所看到的,这适用于博客,因此使用的是Post模型.这是HAML视图模板(app/views/sitemap/index.xml.haml):

- base_url = "http://#{request.host_with_port}"
!!! XML
%urlset{:xmlns => "http://www.sitemaps.org/schemas/sitemap/0.9"}
  - for post in @posts
    %url
      %loc #{base_url}#{post.permalink}
      %lastmod=post.last_modified
      %changefreq monthly
      %priority 0.5
Run Code Online (Sandbox Code Playgroud)

而已!您可以通过在浏览器中显示http:// localhost:3000/sitemap.xml(如果使用Mongrel)或者使用cURL 来测试它.

请注意,stale?如果自上次请求站点地图以来没有新帖子,则控制器使用该方法发出HTTP 304 Not Modified响应.


Nin*_*nad 23

现在对于rails3,最好使用功能齐全的sitemap_generator gem.


Aar*_*ray 19

I love John Topley's answer because it is so simple and elegant, without the need for a gem. But it's a bit dated, so I've updated his answer for Rails 4 and Google Webmaster Tools' latest sitemap guidelines.

config/routes.rb:

get 'sitemap.xml', :to => 'sitemap#index', :defaults => { :format => 'xml' }
Run Code Online (Sandbox Code Playgroud)

app/controllers/sitemap_controller.rb:

class SitemapController < ApplicationController
  layout nil

  def index
    headers['Content-Type'] = 'application/xml'
    last_post = Post.last
    if stale?(:etag => last_post, :last_modified => last_post.updated_at.utc)
      respond_to do |format|
        format.xml { @posts = Post.all }
      end
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

app/views/sitemap/index.xml.haml:

!!! XML
%urlset{:xmlns => "http://www.sitemaps.org/schemas/sitemap/0.9"}
  - for post in @posts
    %url
      %loc #{post_url(post)}/
      %lastmod=post.updated_at.strftime('%Y-%m-%d')
      %changefreq monthly
      %priority 0.5
Run Code Online (Sandbox Code Playgroud)

You can test it by bringing up localhost:3000/sitemap.xml.


小智 10

我建议您查看sitemap_generator gem.它会为你处理所有这些问题...而且,真的,谁想要混淆创作XML?

下面是一个示例站点地图,显示您如何使用Rails模型和路径助手生成站点地图URL:

# config/sitemap.rb
SitemapGenerator::Sitemap.default_host = "http://www.example.com"
SitemapGenerator::Sitemap.create do
  add '/contact_us'
  Content.find_each do |content|
    add content_path(content), :lastmod => content.updated_at
  end
end
Run Code Online (Sandbox Code Playgroud)

然后,您可以根据需要使用Rake任务进行刷新.它真的很简单:)


Las*_*unk 6

这是一个用于在Ruby on Rails中创建站点地图的插件:Ruby on Rails站点地图插件.它负责大部分站点地图逻辑和生成.该插件来自我的主页.

配置示例:

Sitemap::Map.draw do

  # default page size is 50.000 which is the specified maximum at http://sitemaps.org.
  per_page 10

  url root_url, :last_mod => DateTime.now, :change_freq => 'daily', :priority => 1

  new_page!

  Product.all.each do |product|
    url product_url(product), :last_mod => product.updated_at, :change_freq => 'monthly', :priority => 0.8
  end

  new_page!

  autogenerate  :products, :categories,
                :last_mod => :updated_at,
                :change_freq => 'monthly',
                :priority => 0.8

  new_page!

  autogenerate  :users,
                :last_mod => :updated_at,
                :change_freq => lambda { |user| user.very_active? ? 'weekly' : 'monthly' },
                :priority => 0.5

end
Run Code Online (Sandbox Code Playgroud)

最好的问候,Lasse