Created
February 4, 2012 22:43
-
-
Save brentertz/1740803 to your computer and use it in GitHub Desktop.
Rack middleware to redirect WWW to non WWW domain.
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
# Configure Rack middleware | |
config.middleware.insert_before 0, 'WwwRedirect' |
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
class WwwRedirect | |
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.', '//'), 'Content-Type' => 'text/html' }, self] | |
else | |
@app.call env | |
end | |
end | |
def each(&block) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Also see https://gist.github.com/3094260