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

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

问题描述:

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

这是我的app.yaml

This is my app.yaml

runtime: python27
api_version: 1
threadsafe: true

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

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

这是一个静态的angular 2应用程序,我需要将所有未找到的404错误定向到index.html. 有(www)文件夹,并且所有文件都在其中,其中包括index.html.

It's a static angular 2 app , and i need to direct all page not found 404 errors to index.html. There is (www) folder and inside that all file including index.html there.

因此,将其添加为最后一条规则,将在所有其他规则均失败的情况下将其用作index.html

So adding this as the last rule, will cause it to serve index.html if all other rules fail

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

但是我认为您想要的是实际执行重定向.否则,您的基本网址仍将是一些虚假网址.您需要在服务器代码中设置一个基本的请求处理程序来完成此操作(在您的情况下,您的服务器运行时为python27).

But I think what you want is for it to actually perform a redirect; otherwise, your base url will still be some bogus url. You need to setup a basic request handler in server code to do this right (and in your case your server runtime is python27).

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

- url: /.*
  script: main.app

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

And then add a file called main.py with something like this in it:

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)