Redirect Every 404 to a different url


we use an S3 endpoint to serve content.
in case there is a 404 i would like to forward to user to another url. then we can keep track of misses and sometimes generate the content with a script.

would this be possible?

So , you mean you need a Custom 404 Page


Thank you for asking.

Doesn’t S3 offer custom “404” error HTML page? :thinking:
I believe it does have this option as same as Google Cloud Storage.

See here:

When you enable static website hosting for your bucket, you enter the name of the error document (for example, 404.html ). After you enable static website hosting for the bucket, you upload an HTML file with this error document name to your bucket.

Therefore, you can use below code with “HTML redirection” just save it as 404.html and upload to the bucket:

<!DOCTYPE html>
        <title>Redirect S3 404</title>
        <meta charset="utf-8">
        <meta http-equiv="refresh" content="0; URL=" />

You can also use Cloudflare workers (untested example): Custom 404 page with Cloudflare Workers - Mickaël Vieira - Software Engineer #go #javascript #python #rust

1 Like

so this is really weird.

i am trying to use the Cloudflare worker to serve a different 404 page.
my worker code looks as following:

addEventListener("fetch", event => {

async function handleRequest(request) {
  const response = await fetch(request);

  // if the page was not found
  if (response.status === 404) {
    // we return our custom 404 page
    return await fetch("");

  // otherwise we return the original response
  return response;

but the problem is that i am Allways send to the 404 page.
when i change the route of the worker to none. it works perfectly fine again.
i strongly suspects that the Cloudflare worker is adjusting the request somehow.
when i change the code to: if (response.status === 300
i allways get a 404 from the backend.
when i change the route of the worker to my subdomain again. i allways get the 404 again…
please advise.

Maybe because the response from the AWS is HTTP 200 rather than 404, despite the content and the 404.html webpage is returned? :thinking:

Or, you have to first create 404.html to get the HTTP 404 status responded.
That way, Cloudflare Worker would recognize it as 404 from the bucket as the bucket would send the 404 HTTP status, and then the Cloudflare Worker would return/redirect you to where you specified it in Worker script.

Maybe you are missing 404.html file at the bucket first?

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.