Software SEO Ruby on Rails

How to setup sitemap on your website for Google Search indexation

4 min read

Sitemaps help search engines discover and index your public pages. Here's how to set up a sitemap for your Rails app using the sitemap_generator gem.

Installation

Add the gem to your Gemfile:

gem 'sitemap_generator'

Run bundle install, then generate the configuration file:

rails sitemap:install

This creates config/sitemap.rb.

Configuration

Edit config/sitemap.rb to include your public pages:

SitemapGenerator::Sitemap.default_host = "https://yourdomain.com"

SitemapGenerator::Sitemap.create do
  # Marketing pages
  add "/", changefreq: "daily", priority: 1.0
  add "/pricing", changefreq: "weekly", priority: 0.9
  add "/about", changefreq: "monthly", priority: 0.8
  add "/contact", changefreq: "monthly", priority: 0.7

  # Legal pages
  add "/terms", changefreq: "yearly", priority: 0.3
  add "/privacy", changefreq: "yearly", priority: 0.3
end

Important: Only include public pages that don't require authentication. Never add dashboard, admin, or user-specific routes to your sitemap.

Update robots.txt

Add the sitemap location to public/robots.txt:

# Sitemap location
Sitemap: https://yourdomain.com/sitemap.xml.gz

Generate the Sitemap

Run this command to generate your sitemap:

bin/rails sitemap:refresh:no_ping

This creates public/sitemap.xml.gz, which will be automatically served as a static file.

Deployment

For static marketing sites, commit the generated sitemap file:

git add public/sitemap.xml.gz
git commit -m "Add sitemap"

Regenerate and commit whenever you add or remove public pages.

For dynamic sites with frequently changing content, generate the sitemap on the server after deployment or use a scheduled job.

Submit to Google

  1. Visit Google Search Console
  2. Add your property if you haven't already
  3. Go to Sitemaps section
  4. Submit https://yourdomain.com/sitemap.xml.gz

This is how it look like after submission:

Image on google sitempas

That's it! Your sitemap is now helping search engines discover your content.

Back to all posts

About António Eloi

António Eloi

I'm a software engineer based in Portugal with over 7 years of web development experience. I'm passionate about creating software that solves real-world problems and enhances people's lives. Over the years, I have worked both in-office and remotely with individuals from various countries, time zones, and cultures, allowing me to develop strong communication and collaboration skills. Currently, I am part of a great team called Park where we bring solutions to campground management that is both simple and free.