如何配置 Google App Engine yaml 文件来处理 404 错误

How to configure Google App Engine yaml file to handle 404 Error

需要将所有 404 链接重定向到 www 文件夹内的 index.html

这是我的app.yaml

runtime: python27
api_version: 1
threadsafe: true

handlers:
- url: /
  static_files: www/index.html
  upload: www/index.html

- url: /(.*)
  static_files: www/
  upload: www/(.*)

这是一个静态 angular 2 应用程序,我需要将所有页面未找到 404 错误定向到 index.html。 有 (www) 文件夹,里面有所有文件,包括 index.html。

因此将此添加为最后一条规则,如果所有其他规则均失败

,将使其服务index.html
- url: /.*
  static_files: www/index.html
  upload: www/(.*)

但我认为您想要的是它实际执行重定向;否则,你的基础 url 仍然是一些伪造的 url。您需要在服务器代码中设置一个基本的请求处理程序才能正确执行此操作(在您的情况下,您的服务器运行时是 python27)。

因此将此规则添加到 app.yaml

- url: /.*
  script: main.app

然后添加一个名为 main.py 的文件,其中包含如下内容:

import webapp2
app = webapp2.WSGIApplication()

class RedirectToHome(webapp2.RequestHandler):
    def get(self, path):
        self.redirect('/www/index.html')


routes = [
    RedirectRoute('/<path:.*>', RedirectToHome),
]

for r in routes:
    app.router.add(r)