Skip to content

Instantly share code, notes, and snippets.

@veganstraightedge
Created March 27, 2018 22:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save veganstraightedge/0825ba20fc6f413b5e1b32c9485bb339 to your computer and use it in GitHub Desktop.
Save veganstraightedge/0825ba20fc6f413b5e1b32c9485bb339 to your computer and use it in GitHub Desktop.
Redirect `app.any-domain.tld` to `any-domain.tld` while keeping the path in place.
module Rack
class AppRedirect
def initialize(app)
@app = app
end
def call(env)
request = Rack::Request.new(env)
if request.host.start_with?("app.")
location = request.scheme + "://" + request.host.sub("app.", "") + request.path
return redirect(location)
end
@app.call(env)
end
private
def redirect(location)
[
301,
{ "Location" => location, "Content-Type" => "text/html" },
["Moved Permanently"]
]
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment