Custom 404 Page using Cloudflare Workers

In a previous post a solution was suggested to display a custom 404 error page that doesn’t return a 200 HTTP status response. Essentially the solution, explained in this post, was to use a Cloudflare Workers to listen to requests, direct them to the custom 404 page (https://www.site1.com/404.html) and send the appropriate 404 response. I successfully implemented this for one of the three websites in my account using Cloudflare Pages.

My issue is that I dont, however, understand how I can use a second, separate deployment of Cloudflare Workers to direct error requests to https://www/site2.com/404.html: this is because there doesn’t seem to be any obvious way, in Cloudflare Workers, to allocate deployments to a particular website within a Cloudflare account.

Can anyone help?

I’m not going to pretend that I know lots about workers as I’m still new at these myself. Neither have I set up a worker yet for custom 404 pages. But have you tried using 2 different 404 workers (one worker to listen to site1 and another worker to listen to site2), by adding 2 different “trigger → routes” for each worker? This works for me when I need a similar (but slightly different) worker for another website.

That is exactly what I want to do. But how do I set up a worker to listen to site2 rather than then site1? I haven’t been able to work that out.

| hashcakes
April 14 |

  • | - |

I’m not going to pretend that I know lots about workers as I’m still new at these myself. Neither have I set up a worker yet for custom 404 pages. But have you tried using 2 different 404 workers (one worker to listen to site1 and another worker to listen to site2), by adding 2 different “trigger → routes” for each worker? This works for me when I need a similar (but slightly different) worker for another website.

I’m thinking if you use a “404 worker” for your “site1” such as the one at;
Custom 404 page with Cloudflare Workers - Mickaël Vieira - Software Engineer #go #javascript #python #rust
and make 2 copies of this worker, one for “site1” and another for “site2”, and edit/change/save each copy as needed.

Save both of these worker services in Cloudflare, and then go to the CF menus for;
“workers” / click on your “site1” worker / select tab “triggers” / “Add route”
and add a route & zone for “site1”.

Then go back to;
“workers” / click on your “site2” worker / select tab “triggers” / “Add route”
and add a route & zone for “site2”.

Does this not work for you?