Skip to main content

Custom 404 page for a static website

Comments

1 comment

  • Platform.sh DevRel

    Just to add a note about doing this within a NodeJS environment: I was able to get a custom 404 handler working by:

    1. Create a 404.js handler with a wildcard route match - I used ExpressJS, YMMV.
    const config = require("platformsh-config").config();
    const express = require('express')
    const app = express()
    app.get('/*', (req, res) => {
        res.status(404).send('Sorry, that page cannot be found.')
    })
    app.listen(config.port, () => {
        console.log(`[INFO]: ExpressJS listening on ${config.port}.`)
    })
    
    1. Alter app config in .platform.app.yaml to run the 404.js handler script by default, and set passthru to true, to forward any not-found requests to it:
    web:
        commands:
          start: node 404.js
        locations:
          '/':
            root: 'build'
            index: ['index.html']
            scripts: false
            allow: true
            passthru: true
    

    Hope that helps!

    0

Please sign in to leave a comment.