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

主宰稳场 提交于 2019-12-11 01:44:28

问题


Need to redirect all 404 links to index.html inside www folder

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/(.*)

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.


回答1:


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/(.*)

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).

So add this rule to app.yaml

- url: /.*
  script: main.app

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)


来源:https://stackoverflow.com/questions/52856895/how-to-configure-google-app-engine-yaml-file-to-handle-404-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!