by
back

Generate a XML-Sitemap for Google with Ruby on Rails

If you provide a sitemap of your webpage, then it is easier for searchengines to crawl your whole page. You can either create an xml-sitemap or a txt-sitemap.

txt sitemap

A txt sitemap is a simple list of all your pages that searchengines should crawl

/sitemap.txt

http://www.yourpage.com/
http://www.yourpage.com/page1.html
http://www.yourpage.com/page2.html

xml sitemap

With an xml sitemap you can provide more details about every page. You can specify for example the change frequency of a webpage or the date of the last modification of the page. You can help searchengines like google to understand and properly crawl your page.

/sitemap.xml

<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
  <url>
    <loc>http://www.yourpage.com/</loc>
    <lastmod>2014-05-15</lastmod>
    <changefreq>weekly</changefreq>
    <priority>0.7</priority>
  </url>
  <url>
    <loc>http://www.yourpage.com/page1.html</loc>
    <lastmod>2014-04-10</lastmod>
    <changefreq>monthly</changefreq>
    <priority>0.4</priority>
  </url>
  <url>
    <loc>http://www.yourpage.com/page2.html</loc>
    <lastmod>2014-05-11</lastmod>
    <changefreq>monthly</changefreq>
    <priority>0.4</priority>
  </url>
</urlset>

how to generate a sitemap in ruby on rails

First we need a route for our sitemaps.

config/routes.rb

get "sitemap" => "pages#sitemap"

Next we have to select all necessary data that we need to generate all pages.
We can tell the controller, that we want to respond to both formats: xml and txt.
In both cases we don't need a layout as we only want to return text.

app/controllers/pages_controller.rb

def sitemap
    @posts = Post.where(:published => true)
    respond_to do |format|
        format.xml { render layout: false }
        format.txt { render layout: false }
    end
end

For our xml sitemap we can write an xml builder in the view

app/views/pages/sitemap.xml.builder

xml.instruct! :xml, :version => "1.0"
xml.urlset "xmlns" => "http://www.sitemaps.org/schemas/sitemap/0.9" do
  for post in @posts do
    xml.url do
      xml.loc post_url(post)
      xml.lastmod post.updated_at.to_date
      xml.changefreq "monthly"
      xml.priority "0.5"
    end
  end
end

Our txt-sitemap is even easier

app/views/pages/sitemap.text.erb

<% for post in @posts do %>
<%= post_url(post) %>
<% end %>

Now we only need to tell the search engine bots, where they can find the sitemaps. One easy solution is to add a line in the robots.txt file.

public/robots.txt

Sitemap: /sitemap.xml
Sitemap: /sitemap.txt


comments powered by Disqus