Skip to content

Instantly share code, notes, and snippets.

@Hampei
Created March 14, 2012 19:22
Show Gist options
  • Save Hampei/2038818 to your computer and use it in GitHub Desktop.
Save Hampei/2038818 to your computer and use it in GitHub Desktop.
rails rack middelware - redirect from www.site.com to site.com
# 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