I'm using the Tornado with python to build a web server. I want to allow users not to type www
.
For example, if user types example.com
to visit my web page, I want to add www
for user. In other words, if user types example.com
in his browser and type "Enter", a www
will be inserted automatically in front of example.com
.
This is my code for now:
application.add_handlers(r"^(www).*", [(r"/$", IndexHandler)])
With the code above, if user visits www.example.com
, the class IndexHandler
will get the request and render the index page (self.render('/index.html')
).
Then I've tried like this:
application.add_handlers(r"^(example).*", [(r"/$", RedirectionHandler)])
class RedirectionHandler(tornado.web.RequestHandler):
def get(self):
self.redirect('www.example.com')
Well, it doesn't work because self.redirect
gives me example.com/www.example.com
Do
seft.redirect('http://www.example.com')
In general in any language when you redirect plain text, the system thinks it's a relative url and tries to append/complete the url with the existing hostname in the already typed url (here example.com
).
Putting http(s)://
at the beginning tells that the address is absolute, so you rewrite all the address from scratch.