oligopol
oligopol

Reputation: 900

Tornado redirect to a different domain

I have a Tornado server and I want to redirect people coming from a certain country to a totally different domain. It depends on their IP, and I need it to work for every URI chosen. So for example if someone goes to www.mysite.com/about from a British IP, I want to redirect her to www.mysite.uk/about.

I tried adding the function initialize() to the BaseHandler, but according to what I've seen, It's impossible to finish from the init.

I checked out RedirectHandler, but it changes only the URI, and not the entire domain as I need.

Do you know of any solution within Tornado? (I also use nginx, but don't think it can support checking the ip, finding the locations, and also I have a lot of URIs).

Thank you!

Upvotes: 1

Views: 1199

Answers (1)

Ben Darnell
Ben Darnell

Reputation: 22134

RedirectHandler works with both absolute and relative urls; why do you think you can't change the domain with it?

You cannot redirect (or send any response) from initialize(), but you can from prepare(). It sounds like this is the right place for what you want to do:

def prepare(self):
    if should_redirect(self.request):
        self.redirect(new_domain, self.request.uri)
        raise tornado.web.Finish()

Upvotes: 3

Related Questions