I'm working on a GAE application which largely consists of static content. I've configured the following handlers:
- url: /content/(.*\..*)
static_files: static/content/\1
upload: static/content/(.*)
- url: /content/(.+)
static_files: static/content/\1.html
upload: static/content/(.*)\.html
The first handler is used to serve images, stylesheets, etc.; the second handles plain URLs like /content/zoo/monkeys/george
and serves a corresponding HTML file.
Right now, GAE is returning an empty page if there is no corresponding static file for a URL. I'd like to set up a custom 404 page for these cases, but apparently this is not straightforward.
Answers to similar questions suggested putting a "catch-all" handler on the bottom of my app.yaml
, with a RequestHandler
that generates the error page.
However, /content/(.+)
matches all URLs under /content/
, valid or not, which means such a handler won't get invoked.
I can only think of two other solutions:
Is there another way to set up a proper 404 page for this case?
You can use Google App Engine to host a static website. Static web pages can contain client-side technologies such as HTML, CSS, and JavaScript.
The App Engine standard environment is based on container instances running on Google's infrastructure. Containers are preconfigured with one of several available runtimes. The standard environment makes it easy to build and deploy an application that runs reliably even under heavy load and with large amounts of data.
Go to the App Engine dashboard on the Google Cloud Platform Console and press the Create button. If you've not created a project before, you'll need to select whether you want to receive email updates or not, agree to the Terms of Service, and then you should be able to continue.
Answer to an old thread for those that would find it useful.
Add require_matching_file: true
as the last property of the handler.
If there is no corresponding file, the next handler in the waterfall will be invoked, instead of generating the default 404
page.
Then add a new catch-all section at the bottom of app.yaml
with a dynamic handler that processes invalid URLs
Example:
- url: /content/(.*\..*)
static_files: static/content/\1
upload: static/content/(.*)
require_matching_file: true
- url: /content/(.+)
static_files: static/content/\1.html
upload: static/content/(.*)\.html
require_matching_file: true
- url: /.*
script: auto
This way GAE will serve all existing static assets through the first 2 handlers. The dynamic handler will be invoked only for non-existing URLs. You will need to write the code for that obviously...
One big disclaimer here : require_matching_file
is undocumented. But it turns out that GAE adds that field automatically to your app.yaml
when you upload it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With