Skip to content

Instantly share code, notes, and snippets.

@ivan-leschinsky
Forked from brentertz/application.rb
Last active August 29, 2015 14:07
Show Gist options
  • Save ivan-leschinsky/89fe44b11aa34ace1406 to your computer and use it in GitHub Desktop.
Save ivan-leschinsky/89fe44b11aa34ace1406 to your computer and use it in GitHub Desktop.
Middleware for robots.txt disallowing
# 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')
@ivan-leschinsky
Copy link
Author

brentertz says:

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