2009-06-29 34 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) 
相关问题