Google App Engine Python Webapp2 301从www重定向到非www域

use*_*ond 1 python seo google-app-engine no-www webapp2

我有一个建立在gae上的应用程序.我使用python和webapp2框架.我需要将301重定向从www.my-crazy-domain.com重定向到my-crazy.domain.com,以便在搜索结果中消除www和not-www双打.

有没有人有现成的解决方案?谢谢你的帮助!

use*_*ond 5

我成功了.

class BaseController(webapp2.RequestHandler):
   """
   Base controller, all contollers in my cms extends it
   """

    def initialize(self, request, response):
        super(BaseController, self).initialize(request, response)
        if request.host_url != config.host_full:
            # get request params without domain
            url = request.url.replace(request.host_url, '')
            return self.redirect(config.host_full+url, permanent=True)
Run Code Online (Sandbox Code Playgroud)

config.host_full包含我没有www的主域名.解决方案是检查基本控制器中的请求,如果域不同则进行重定向.

  • 感谢您发布解决方案(而不是像别人说的那样删除它!),这对我有所帮助!:) (2认同)