Saurabh Tripathi
Saurabh Tripathi

Reputation: 111

How to add custom error page for 404 error (page not found)

I am trying to add a custom 404 error page in my website for "Page Not Found" or putting wrong URL. I have app.yaml file and used Go language. My website is basically in Google App Engine. I have checked each and every example which I think its related to my problem in stack overflow. But in some example they explained example by Python or Java language but I want to find in Go language. because my code is written in Go.

The static error 404 page is loaded in server but when I filled wrong URL then it show error page not found instead my custom page. I have attached screenshot which tells you everything.

current:- https://i.sstatic.net/7ayje.jpg "This is what current showing"

Required:- https://i.sstatic.net/jrapU.jpg "This is what i want"

I have read google app engine's app.yaml documents and tried to put error-handlers but i did not find any solution.

app.yaml file Code

runtime: go
api_version: go1

handlers:

-url: /

  static_files: www/index.html

  upload: www/index.html

-url: /(.*)

  static_files: www/\1

  upload: www/(.*)

error_handlers: 

  -file: www/page-not-found.html* 

Here's the updated app.yaml file:

runtime: go

api_version: go1

error_handlers: 
  - file: /page-not-found.html

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

Upvotes: 0

Views: 1018

Answers (1)

icza
icza

Reputation: 418565

Documentation of the app.yaml configuration file for Go apps can be found at app.yaml Configuration File.

Search for error_handlers on that page to see how it should be defined. It has the following red warning:

Warning: Make sure that the path to the error response file does not overlap with static file handler paths.

You specified:

-file: www/page-not-found.html*

This overlaps with one of your static handler paths:

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

That's why your custom error page doesn't get served. You must place your error handler outside of the www folder (or not use the whole www folder as a static handler path, what matters is they can't overlap).

Note: I linked to the Go 1.12 app configuration, because Go 1.9 which you're using is depcerated and soon will become unavailable. Nonetheless, the same applies to both versions regarding custom error pages.

Upvotes: 1

Related Questions