如何从appspot域重定向到自定义域?

时间:2011-12-29 23:44:42

标签: google-app-engine web2py

我发现Amir发布了有关将请求从google.appspot域重定向到自定义域的帖子。我的问题是你在哪里用Web2py写这样的东西?

**To just add a custom domain, just follow the instructions here: http://code.google.com/appengine/articles/domains.html
And once that works, you can put a check in your code to forward anyone landing on the appspot.com domain to your domain: (example in python)
def get(self):
  if self.request.host.endswith('appspot.com'):
    return self.redirect('www.jaavuu.com', True)
  # ... your code ...**

3 个答案:

答案 0 :(得分:3)

在第一个模型文件的开头,您可以执行以下操作:

if request.env.http_host.endswith('appspot.com'):
    redirect(URL(host='www.yourdomain.com', args=request.args, vars=request.vars))

除了用www.yourdomain.com替换yourdomain.appspot.com外,这将保留整个原始网址。注意,URL()将自动填写当前的控制器和函数,但您必须显式传递当前的request.args和request.vars以确保它们得到保留。

答案 1 :(得分:1)

这将进入您的请求处理程序。

使用web2py documentation中的示例:

例8

  

在控制器中:simple_examples.py

def redirectme():
    redirect(URL('hello3'))

你想做这样的事情:

def some_function():
    if request.env.http_host.endswith('appspot.com'):
        redirect(URL('www.yourdomain.com'))

答案 2 :(得分:0)

使用webapp2就像我做的那样,其中BaseHandler是我所有处理程序的类型:

class BaseHandler(webapp2.RequestHandler):
    def __init__(self, request, response):
        self.initialize(request, response)
        if request.host.endswith('appspot.com'):
            query_string = self.request.query_string
            redirect_to = 'https://www.example.com' + self.request.path + ("?" + query_string if query_string else "")
            self.redirect(redirect_to, permanent=True, abort=True)
相关问题