Search code examples
ruby-on-railshttp-redirecthost

How to get redirect_to to change host when using a path as a string?


I'm redirecting in rails using a string like so:

path = "/blerg/blergs/blergs/blergs"

domain = "www.google.com"

So in the controller I'm redirect like this:

redirect_to path, :status => :found, :host => domain

The problem is the domain isn't changing, it's staying the same as the app's domain. How do redirect with the host param while using a string as the path?

The reason I'm doing this to avoid this security bug, the path has some params in it too: http://brakemanscanner.org/docs/warning_types/redirect/


Solution

  • As you can see here, when you use string paths, rails use request's domain.

    one way to tell rails that url you are passing is in another domain, is to set the protocol:

         url = "http://#{another_domain_url}"
         redirect_to url
    

    If you want to use a hash, you can move path to :host key (not tested):

         redirect_to :status => :found, :host => "#{domain}#{path}"