Skip to content

Instantly share code, notes, and snippets.

@osulyanov
Forked from moiristo/gist:1245170
Created February 12, 2013 12:12
Show Gist options
  • Save osulyanov/4761923 to your computer and use it in GitHub Desktop.
Save osulyanov/4761923 to your computer and use it in GitHub Desktop.
Rails3 way to redirect non-www domain to www domain
# Rails3 way to redirect non-www domain to www domain
# Single domain redirect
'example.com'.tap do |host|
constraints(:host => host) do
match '/(*path)', :to => redirect { |params, request| Addressable::URI.escape request.url.sub(host, "www.#{host}") }
end
end
# Multiple domain redirect
['host-1.com', 'host-2.com'].each do |host|
constraints(:host => host) do
match '/(*path)', :to => redirect { |params, request| Addressable::URI.escape request.url.sub(host, "www.#{host}") }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment