Skip to content

Instantly share code, notes, and snippets.

@dignoe
Created October 31, 2012 20:10
Show Gist options
  • Save dignoe/3989505 to your computer and use it in GitHub Desktop.
Save dignoe/3989505 to your computer and use it in GitHub Desktop.
Redirect all domains to specified domain in Rails 3
MyApp::Application.routes.draw do
if Rails.env.production?
constraints(:host => /^(?!www\.my-domain\.com)/) do
match "(*path)" => redirect { |params, request| URI.parse(request.url).tap { |x| x.host = "www.my-domain.com" }.to_s}
end
end
end
@dignoe
Copy link
Author

dignoe commented Oct 31, 2012

Use this to redirect non www URLs to your domain. In addition, it redirects any domain that is not the one you specified (i.e. 192.168.0.1, ec-192-168-0-1.amazon-aws.com, etc.).

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