2009-06-29 30 views
16

如何配置app.yaml文件以將所有網址重定向到另一個網址?例如,我想要http://test.appspot.com/hellohttp://test.appspot.com/hello28928723重定向到http://domain.com如何使用Google App Engine重定向所有網址

我目前只提供靜態文件。這是我的app.yaml文件:

application: testapp 
version: 1 
runtime: python 
api_version: 1 

handlers: 
- url: (.*)/ 
    static_files: static\1/index.html 
    upload: static/index.html 

- url:/
    static_dir: static 

回答

7

您可以輕鬆地用python處理程序重定向所有請求。像

class FormHandler(webapp.RequestHandler): 
    def post(self): 
    if processFormData(self.request): 
     self.redirect("http://domain.com") 
+7

此答案不完整 - 該代碼片段本身不足以重定向reqeusts。另外,獲取請求呢? – 2012-05-01 22:50:07

+1

@BrianLeathem剛剛發佈了一個[完整示例](http://stackoverflow.com/a/18297787/282729)。 – feklee 2013-08-18 09:50:57

33

webapp2的東西有一個內置的重定向處理

沒有必要推出自己的處理程序; webapp2已經有一個。

application = webapp2.WSGIApplication([ 
    webapp2.Route('/hello', webapp2.RedirectHandler, defaults={'_uri':'http://domain.com'}), 
    webapp2.Route('/hello28928723', webapp2.RedirectHandler, defaults={'_uri':'http://domain.com'}), 
], debug=False) 

_uri參數是RedirectHandler類用來定義目標的參數。花了很多Google Fu找到這個文檔,但它在我的應用程序中完美地工作。

更新:

我以爲你意識到這一點,但你需要從改變你的包羅萬象的路線:

- url:/
    static_dir: static 

要(python27版):

- url: /.* 
    script: main.application 

或者:(pre python27 version)

- url: /.* 
    script: main.py 

main.py是包含請求處理程序+路由的文件。

注意:由於靜態文件的性質,沒有靜態方法來處理GAE上的重定向。基本上,沒有辦法單獨在app.yaml中進行重定向。

+1

這處理get/post/etc ...這基本上是在GAE中執行301(永久)重定向的「正確」方法。 – 2012-05-04 17:34:46

+0

重要的是要注意,顯然App Engine不支持重定向到靜態文件/目錄。我無法讓重定向工作。我一直在我的日誌中發現這些錯誤:「未找到處理程序引用的文件:main.app」。最後,我發現這一點,確認靜態文件無法被處理程序訪問:http://stackoverflow.com/questions/8734040/google-app-engine-static-pages-python-2-5-directories-etc。 – 2013-11-27 19:20:12

2

這裏是一個Python腳本,將做重定向:

from google.appengine.ext import webapp 
from google.appengine.ext.webapp.util import run_wsgi_app 

class MainPage(webapp.RequestHandler): 
    def get(self, path): 
    self.redirect("http://example.com", permanent=True) 
    def head(self, path): 
    self.redirect("http://example.com", permanent=True) 

application = webapp.WSGIApplication(
      [ 
       (r'^(.*)', MainPage) 
      ]) 

def main(): 
    run_wsgi_app(application) 

if __name__ == "__main__": 
    main() 
7

所有你需要(替換app-idhttp://example.com):

  • app.yaml

    application: app-id 
    version: 1 
    runtime: python27 
    api_version: 1 
    threadsafe: false 
    
    handlers: 
    - url: /.* 
        script: main.py 
    
  • main.py

    from google.appengine.ext import webapp 
    from google.appengine.ext.webapp.util import run_wsgi_app 
    
    class AllHandler(webapp.RequestHandler): 
        def get(self): 
         self.redirect("http://example.com", True) 
    
    application = webapp.WSGIApplication([('/.*', AllHandler)]) 
    
    def main(): 
        run_wsgi_app(application) 
    
    if __name__ == "__main__": 
        main() 
    
1

如果你想有一個靜態文件只的方式做 「重定向」,那麼這樣做:

在應用程序中。YAML,把這個作爲包羅萬象的,在文件的結尾:

- url: /.* 
    static_files: root/redirect.html 
    upload: root/redirect.html 

然後在根目錄/文件將redirect.html,把這個:

<!DOCTYPE html> 
<html> 
    <head> 
     <meta http-equiv="refresh" content="0;URL=/" /> 
     <script> 
      location.replace("/"); 
     </script> 
    </head> 
    <body></body> 
</html> 

這個例子將重定向所有未知到根的URL(即/)。如果您想要另一個網址,只需在相應的地方替換http://mydomain.com即可。

0

的前一個答案中提到的webapp2的重定向處理程序(webapp2.RedirectHandler)不適用於發佈請求,因爲它不包含post方法(請參閱https://github.com/GoogleCloudPlatform/webapp2/blob/master/webapp2.py),所以如果關注帖子,您將需要滾動您自己的python處理程序。類似以下內容:

import webapp2 

class MainPage(webapp2.RequestHandler): 


    def post(self): 
     self.redirect('http://example.com') 


application = webapp2.WSGIApplication([ 
('/.*', MainPage) 
], debug=False) 
相關問題