Created
August 29, 2013 05:43
-
-
Save deanet/6374603 to your computer and use it in GitHub Desktop.
domain redirect Ror
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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