2010-02-24 72 views
1

如何將500個錯誤從google appengine重定向到另一個位置。如何將AppEngine 500錯誤重定向到其他位置?

以下是一個示例方案:

在500錯誤的情況下的所有請求,以http://example.appspot.com/testfile.ext應該重定向到http://www.example.com/testfile.ext

這基本上是500錯誤與302重定向組合。有沒有可能的解決方法?

+0

你能解釋一下你爲什麼要這樣做嗎?在錯誤的情況下返回非500錯誤代碼通常是一個可怕的想法 - 也許有更好的方法來做你所要求的。 – 2010-02-25 10:16:34

+0

其實我試圖創建一個失敗保險箱。如果用戶嚮應用引擎發出請求,並由於某種原因引發500錯誤,請求應自動重定向到我的備份服務器位置。這只是爲了確保故障安全。 – GeekTantra 2010-02-25 10:26:09

回答

0

它可以重定向用最簡單的方法失敗,我知道

try: 
    #stuff  
    except: 
    self.redirect('http://www.example.com/testfile.ext') 
    return 

還是這個稍微複雜一點的重定向可以修改從301到302

def redirect_from_appspot(wsgi_app): 
    def redirect_if_needed(env, start_response): 
     if env["HTTP_HOST"].startswith('my_app_name.appspot.com'): 
      import webob, urlparse 
      request = webob.Request(env) 
      scheme, netloc, path, query, fragment = urlparse.urlsplit(request.url) 
      url = urlparse.urlunsplit([scheme, 'www.my_domain.com', path, query, fragment]) 
      start_response('301 Moved Permanently', [('Location', url)]) 
      return ["301 Moved Peramanently", 
        "Click Here" % url] 
     else: 
      return wsgi_app(env, start_response) 
    return redirect_if_needed 

案例取決於如果500錯誤是你的應用程序或谷歌服務器。最簡單的方法是在gae控制檯中使用它,我們可以請求應用程序引擎團隊啓用,或者使用yaml或者使用正則表達式進行最正式的配置。簡單的方法是嘗試,除了重定向。

+0

您告訴的方法只適用於以個案爲單位處理錯誤。沒有一種方法可以處理我的應用程序中的所有500個錯誤。 – GeekTantra 2010-02-25 03:12:52

相關問題