How To Create A Dynamic Robots.txt In Rails

Posted by Weston Ganger

I needed to have a dynamic robots.txt because I was serving two different sites from one Rails application. One common case is the need to block robots in the staging environment. In this article we will set this up.

First we set up a route for the robots:

# config/routes.rb
get '/robots.:format' => 'pages#robots'

Then in the pages controller or whichever controller you used above

# app/controllers/pages_controller.rb
class PagesController < ApplicationController
  def robots
    respond_to :text
    expires_in 6.hours, public: true

Then you create your erb/slim/haml view which is your robots.txt file:

# app/views/pages/robots.html.erb
<% if Rails.env.production? %>
  User-Agent: *
  Allow: /
  Disallow: /admin
<% else %>
  User-Agent: *
  Disallow: /
<% end %>

Now the robots will be disallowed for the entire app unless its running in the production environment.

Related External Links:

Article Topic:Software Development - Rails

Date:July 28, 2016

Recommended Posts



Send Me A Message

I would love to get in touch and talk about your next project. Feel free to send a message and I will get back to you shortly.

Get Connected