Skip to content

Instantly share code, notes, and snippets.

@brentertz
Last active October 13, 2015 06:58
Show Gist options
  • Save brentertz/4157181 to your computer and use it in GitHub Desktop.
Save brentertz/4157181 to your computer and use it in GitHub Desktop.
Disallow site indexing in non-production environments
# Disallow site indexing in non-production environments
config.middleware.insert_before(::Rack::Lock, '::Rack::Robots')
require 'rack'
# Disallow site indexing in non-production environments
module Rack
class Robots
def initialize(app)
@app = app
end
def call(env)
if env['PATH_INFO'] == '/robots.txt' && !([ENV['RAILS_ENV'], ENV['RACK_ENV']].include? 'production')
[200, { 'Content-Type' => 'text/plain' }, ["User-Agent: *\nDisallow: /"]]
else
@app.call env
end
end
end
end
# Disallow site indexing in non-production environments
Rails.application.config.middleware.insert_before(::Rack::Lock, '::Rack::Robots')
@brentertz
Copy link
Author

Can load either via application.rb or robots_initializer.rb

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