Skip to content

Instantly share code, notes, and snippets.

@moiristo
Created September 27, 2011 14:27
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save moiristo/1245170 to your computer and use it in GitHub Desktop.
Save moiristo/1245170 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
@chris-allnutt
Copy link

thanks for this solution, helped me find the wrong number of arguments error I was having with the railsdog solution

@sahin
Copy link

sahin commented Oct 12, 2012

thanks

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