Google App Engine and 404 error

前端 未结 9 1223
一生所求
一生所求 2020-12-02 06:46

I\'ve setup a static website on GAE using hints found elsewhere, but can\'t figure out how to return a 404 error. My app.yaml file looks like

- url: (.*)/
           


        
9条回答
  •  南方客
    南方客 (楼主)
    2020-12-02 07:00

    I have reviewed all the above given answers and used the following at the end as the most universal 404 solution:

    Add this link at the end of app.yaml

    - url: /(.*) 
      script: 404.app
    

    and create 404.py with the following content

    import webapp2
    from google.appengine.ext.webapp import template
    
    class NotFound(webapp2.RequestHandler):
      def get(self):
        self.error(404)
        self.response.out.write(template.render('404.html', {}))
    
    app = webapp2.WSGIApplication([
        ('/.*', NotFound)
    ], debug=True)
    

    This will display contents of 404.html file with 404 error code.

    The advantage of this solution is simplicity, correctness of bahaviour and flexibility, as it allows to use a static 404.html file as error page content.

    I also want to warn against some of the solutions suggested above.

    • Custom Error Responses don not work with 404 error
    • Also don't use static files, as they would return 200 instead of 404 error. This can cause a lot of headache ahead.

提交回复
热议问题