Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
class PreferredVersionRedirect
def initialize(app)
@app = app
end
def call(env)
req = Rack::Request.new(env)
if valid?(req)
@app.call(env)
else
redirect(req)
end
end
def valid?(req)
req.ssl? && req.host == "example.com"
end
def redirect(req)
location = URI.join("https://example.com", req.fullpath)
[301, {"Location" => location.to_s, "Content-Type" => "text/html"}, ["Moved Permanently"]]
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment