user2378683
user2378683

Reputation: 11

Simple static website in GAE with custom 404 error page

I am using GAE for a simple static website with just html/htm pages, pictures etc. I am also using Python 2.7.

So i use a straight forward app.yaml and main.py and that works. However, when accessing a page which does not exist, it shows a standard 404 page. I want to change that one into custom error page, and tried this below but it does not work.

here are my app.yaml and main.py files:

application: xxxx
version: 11
runtime: python27
api_version: 1
threadsafe: true

default_expiration: "7d"

inbound_services:
- warmup

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

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

- url: /.*
  script: main.app

Main.py:

import webapp2

class BaseHandler(webapp2.RequestHandler):
  def handle_exception(self, exception, debug):
    # Set a custom message.
    self.response.write('An error occurred.')

    # If the exception is a HTTPException, use its error code.
    # Otherwise use a generic 500 error code.
    if isinstance(exception, webapp2.HTTPException):
      self.response.set_status(exception.code)
    else:
      self.response.set_status(500)

class MissingPage(BaseHandler):
  def get(self):
    self.response.set_status(404)
    self.response.write('Page has moved. Pls look at http://www.yyyyyy.yy to find the new location.')

class IndexHandler(webapp2.RequestHandler):
    def get(self):
        if self.request.url.endswith('/'):
            path = '%sindex.html'%self.request.url
        else:
            path = '%s/index.html'%self.request.url

        self.redirect(path)

    def post(self):
        self.get()

app = webapp2.WSGIApplication(
   [  (r'/', IndexHandler),
      (r'/.*', MissingPage)
      ],
   debug=True)

What is not correct?? I find a lot of entries, but none exactly explains how to do this for a simple website with Python 2.7,

let me know, many thanks, Michael

Upvotes: 1

Views: 1178

Answers (1)

lucemia
lucemia

Reputation: 6617

it looks like it doesn't really need to have any dynamic part of your website except the 404 page. There is an error_handlers can be used directly.

https://developers.google.com/appengine/docs/python/config/appconfig#Custom_Error_Responses

application: xxxx
version: 11
runtime: python27
api_version: 1
threadsafe: true

default_expiration: "7d"

inbound_services:
- warmup

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

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

error_handlers:
- file: default_error.html

Upvotes: 2

Related Questions