Skip to content

Instantly share code, notes, and snippets.

@deanet
Created August 29, 2013 05:43
Show Gist options
  • Save deanet/6374603 to your computer and use it in GitHub Desktop.
Save deanet/6374603 to your computer and use it in GitHub Desktop.
domain redirect Ror
module Rack
# Automatically redirects to the configurable domain
#
# If request comes from other than specified domains it redirects to the first
# domain from the list
class DomainRedirect
def initialize(app, hosts = [])
@app = app
@hosts = hosts
end
def call(env)
req = Rack::Request.new(env)
if @hosts.nil? or @hosts.empty? or @hosts.include?(req.host)
@app.call(env)
else
url = "http://#{@hosts[0]}"
# url << ":#{req.port}" unless req.port == 80
url << "#{req.path}"
url << "?#{req.query_string}" unless req.query_string.empty?
res = Rack::Response.new
res.redirect(url)
res.finish
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment