龙卷风:如果用户没有输入,如何添加www

时间:2017-07-07 06:56:50

标签: python tornado

我使用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

1 个答案:

答案 0 :(得分:0)

待办事项

seft.redirect('http://www.example.com')

一般情况下,当您重定向纯文本时,系统会认为它是相对网址,并尝试使用已输入网址(此处为example.com)中的现有主机名追加/填写网址。

http(s)://放在开头告诉地址是绝对的,所以你从头开始重写所有地址。