【问题标题】:How to redirect all URLs with Google App Engine如何使用 Google App Engine 重定向所有 URL
【发布时间】:2010-11-06 16:26:55
【问题描述】:

如何配置 app.yaml 文件以将所有 URL 重定向到另一个 URL?例如我希望http://test.appspot.com/hello 或http://test.appspot.com/hello28928723 重定向到http://domain.com。

我目前只提供静态文件。这是我的 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

【问题讨论】:

    标签: google-app-engine yaml


    【解决方案1】:

    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
    

    或者:(python27之前的版本)

    - url: /.*
      script: main.py
    

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

    注意:由于静态文件的性质,在 GAE 上没有处理重定向的纯静态方法。基本上,没有办法单独在 app.yaml 中进行重定向。

    【讨论】:

    • 这处理 get/post/etc... 这基本上是在 GAE 中进行 301(永久)重定向的“正确”方式。
    • 请务必注意,App Engine 显然不支持重定向到静态文件/目录。我无法让重定向工作。我的日志中不断出现这些错误:“找不到处理程序引用的文件:main.app”。最后,我发现了这一点,这证实了处理程序无法访问静态文件:stackoverflow.com/questions/8734040/…。
    • @StephenKaiser 从技术上讲,这不是重定向,但您可以在 app.yaml 中定义一个从旧 URI 指向新 URI 的新路由。由于架构和处理静态文件的方式,无法在静态文件上定义实际的 301/302 重定向。
    • 我认为 hello28928723 只是一个例子,他想重定向所有请求。
    • @Jonny 在 GAE 上处理静态和非静态文件的方式存在显着差异。您可以在静态处理程序上方的列表中添加一个非静态句柄,专门用于捕获重定向。无论哪种方式,了解 GAE 中处理静态文件的方式和原因都不同,这一点很重要。 App.yaml 基本上是用于反向代理的指令集,其中所有静态文件实际上都存储在与非静态文件分开的静态文件缓存中。在您访问 http 服务器(即不是静态缓存)之前,无法进行 302/301 重定向。
    【解决方案2】:

    所有你需要的(替换app-id,http://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()
      

    【讨论】:

    【解决方案3】:

    您可以使用 python 处理程序轻松重定向所有请求。类似的东西

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

    【讨论】:

    • 这个答案不完整 - 代码 sn-p 本身不足以重定向请求。另外,get 请求呢?
    • @BrianLeathem 刚刚发布了complete example。
    【解决方案4】:

    这是一个执行重定向的 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()
    

    【讨论】:

    • 请注意@Evan Plaice 提供的 webapp2 答案有效,但我被旧数据存储卡住了,无法使用 python 2.7。
    • +1 用于发布与 webapp 向后兼容的解决方案。
    【解决方案5】:

    如果您想要 仅静态文件 方式来执行“重定向”,请执行以下操作:

    在 app.yaml 中,将其作为总括,放在文件末尾:

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

    然后在 root/redirect.html 文件中,放这个:

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

    此示例会将所有未知 URL 重定向到根目录(即 / )。如果您想要另一个 URL,只需在适当的位置替换 http://mydomain.com。

    【讨论】:

      【解决方案6】:

      上一个答案中提到的 webapp2 (webapp2.RedirectHandler) 的重定向处理程序不适用于 post 请求,因为它不包含 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)
      

      【讨论】:

        【解决方案7】:

        引用 Evan 的回答,重定向所有请求,您可以使用正则表达式执行以下操作:

        import webapp2
        from webapp2_extras.routes import RedirectRoute
        
        app = webapp2.WSGIApplication([
             RedirectRoute('/<:.*>', redirect_to='/')
            ], debug=False)
        

        查看官方文档:

        https://webapp2.readthedocs.io/en/latest/guide/routing.html

        https://webapp2.readthedocs.io/en/latest/api/webapp2.html#webapp2.Route.init

        【讨论】:

        猜你喜欢
        • 2012-10-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-05-17
        • 1970-01-01
        • 1970-01-01
        • 2011-04-14
        • 1970-01-01
        相关资源
        最近更新 更多