Created
March 14, 2012 19:22
-
-
Save Hampei/2038818 to your computer and use it in GitHub Desktop.
rails rack middelware - redirect from www.site.com to site.com
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
# in application.rb add | |
# config.middleware.insert 0, "RedirectFromWww" # first, so you don't need a certificate for the www. version | |
class RedirectFromWww | |
def initialize(app) | |
@app = app | |
end | |
def call(env) | |
request = Rack::Request.new(env) | |
if request.host.starts_with?("www.") | |
[301, {"Location" => request.url.sub("//www.", "//")}, []] | |
else | |
@app.call(env) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment