我使用Tornado和python构建一个Web服务器。我想允许用户不要输入www
。
例如,如果用户输入example.com
来访问我的网页,我想为用户添加www
。换句话说,如果用户在其浏览器中键入example.com
并输入"输入",www
将自动插入example.com
前面。 / p>
这是我现在的代码:
application.add_handlers(r"^(www).*", [(r"/$", IndexHandler)])
使用上面的代码,如果用户访问www.example.com
,则类IndexHandler
将获取请求并呈现索引页(self.render('/index.html')
)。
然后我尝试过这样的话:
application.add_handlers(r"^(example).*", [(r"/$", RedirectionHandler)])
class RedirectionHandler(tornado.web.RequestHandler):
def get(self):
self.redirect('www.example.com')
嗯,它没有用,因为self.redirect
给了我example.com/www.example.com
答案 0 :(得分:0)
待办事项
seft.redirect('http://www.example.com')
一般情况下,当您重定向纯文本时,系统会认为它是相对网址,并尝试使用已输入网址(此处为example.com
)中的现有主机名追加/填写网址。
将http(s)://
放在开头告诉地址是绝对的,所以你从头开始重写所有地址。