Code father
Code father

Reputation: 605

How can I redirect to different host?

I have users that are from different countries. Based on the user country we have two different sites 'test.com.au' and 'test.co.uk'. Is there a way I could redirect users from au to 'test.com.au' and uk to 'test.co.uk' as in

def test_method
 user = User.find(params[:id])
 if user.country == 'Australia'
  host = 'test.com.au'
 else
  host = 'test.co.uk'
 end

 redirect_to user_path # On the host provided
end

Upvotes: 1

Views: 256

Answers (1)

lacostenycoder
lacostenycoder

Reputation: 11226

In theory you should be able to do this, however I don't know that you'll be able to maintain a session across root domains, but if there is no authentication, perhaps this will work:

def test_method
  user = User.find(params[:id])
  if user.country == 'Australia'
    host = 'test.com.au'
  else
    host = 'test.co.uk'
  end

  redirect_to (request.protocol + host + user_path)

end

Upvotes: 2

Related Questions