21

如何配置 app.yaml 文件以将所有 URL 重定向到另一个 URL?例如,我希望http://test.appspot.com/hellohttp://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
4

7 回答 7

40

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 中进行重定向。

于 2012-02-24T09:24:06.077 回答
9

所有你需要的(替换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()
    
于 2013-08-18T09:42:36.773 回答
5

您可以使用 python 处理程序轻松重定向所有请求。就像是

class FormHandler(webapp.RequestHandler):
  def post(self):
    if processFormData(self.request):
      self.redirect("http://domain.com")
于 2009-06-29T15:09:23.047 回答
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()
于 2012-05-01T13:30:21.430 回答
4

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

在 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

于 2014-06-06T19:27:28.717 回答
0

上一个答案中提到的 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)
于 2017-09-09T12:37:56.620 回答
0

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

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。在里面

于 2015-09-03T09:52:25.090 回答