Skip to content

Instantly share code, notes, and snippets.

@dtuite
Forked from timcheadle/README.md
Created April 11, 2014 15:21
Show Gist options
  • Save dtuite/10477408 to your computer and use it in GitHub Desktop.
Save dtuite/10477408 to your computer and use it in GitHub Desktop.
Environment aware robots.txt for Rails

Make /robots.txt aware of the Rails environment

You probably don't want Google crawling your development staging app. Here's how to fix that.

$ mv public/robots.txt config/robots.production.txt
$ cp config/robots.production.txt config/robots.development.txt

Now edit config/routes.rb to add a route for /robots.txt, and add the controller code.

def robots
robots = File.read(Rails.root.join("config", "robots.#{Rails.env}.txt"))
render :text => robots, :layout => false, :content_type => "text/plain"
end
# (moved from public/robots.txt)
#
# See http://www.robotstxt.org/wc/norobots.html for documentation on how to use the robots.txt file
#
# To ban all spiders from the entire site uncomment the next two lines:
User-Agent: *
Disallow: /
# (moved from public/robots.txt)
#
# See http://www.robotstxt.org/wc/norobots.html for documentation on how to use the robots.txt file
#
# To ban all spiders from the entire site uncomment the next two lines:
# User-Agent: *
# Disallow: /
get '/robots.txt' => 'home#robots'
@malayhm
Copy link

malayhm commented Jun 10, 2020

If we want to disable the search engine crawlers to scrap the website, then there is a simple alternative:

<% if !Rails.env.production? %>
  <meta name="robots" content="noindex,nofollow">
<% end %>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment