2011-12-29 39 views
1

我從Amir發現此帖是關於將請求從google.appspot域重定向到自定義域。我的問題是你在哪裏使用Web2py這樣的東西?如何將appspot域重定向到自定義域?

**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

在你的第一個模型文件的開頭,你可以這樣做:

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

這樣會保留完整的原始URL,只是用www.yourdomain.com更換yourdomain.appspot.com。請注意,URL()將自動填入當前的控制器和函數,但您必須顯式傳遞當前的request.args和request.vars以確保它們得到保留。

+0

非常感謝你! – 2011-12-31 08:05:51

+0

@Anthony:在項目應用程序中何處保留此代碼? – 2016-02-08 17:51:10

+0

正如答案中所指出的那樣,在第一個模型文件(模型文件按字母順序執行)開始時 - 這樣,它將在任何其他應用程序代碼運行之前重定向。 – Anthony 2016-02-08 17:58:16

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')) 
+0

注意,在'URL(「www.yourdomain.com」)',將web2py的解釋「 www.yourdomain.com'作爲控制器,並將生成一個URL,如'/yourapp/www.yourdomain.com'。相反,你需要'URL(host ='www.yourdomain.com')'。另外,最好把它放在模型中(在函數之外)。請參閱http://stackoverflow.com/a/8681704/440323。 – Anthony 2011-12-30 16:38:55

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) 
相關問題