Skip to content

Instantly share code, notes, and snippets.

@brentertz
Created February 4, 2012 22:43
Show Gist options
  • Save brentertz/1740803 to your computer and use it in GitHub Desktop.
Save brentertz/1740803 to your computer and use it in GitHub Desktop.
Rack middleware to redirect WWW to non WWW domain.
# Configure Rack middleware
config.middleware.insert_before 0, 'WwwRedirect'
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
@brentertz
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment