Cloudflare R2 custom domain returns 404 — a Worker wildcard route was eating it
Everything reported healthy. The bucket, the files, the custom domain, the DNS record. And every request to media.example.com returned a 404. It cost me most of a day, so here it is in full. The symptom I had 60 video files in an R2 bucket, served through a custom domain — media.example.com. Every request returned 404. Not a Cloudflare error page, not an XML error from the bucket. A 404. The…
Cloudflare's R2 custom domain was returning a 404 error due to a Worker wildcard route that was intercepting the requests. Despite everything appearing healthy, the bucket, files, custom domain, and DNS record were all functioning correctly. The issue stemmed from the Worker route taking precedence over the R2 custom domain for the same hostname.
A Worker route with a wildcard pattern (.example.com/) was set up earlier and overshadowed the R2 custom domain's functionality for video files. Consequently, when a request was made to media.example.com/clip-01.mp4, the Worker attempted to find a static asset at /clip-01.mp4 but couldn't find it, hence serving the site's default 404 page instead.
The R2 dashboard displayed everything working correctly because it wasn't receiving any requests. To resolve the issue, it was necessary to stop using the wildcard route and explicitly define the hostnames that the Worker should serve, such as example.com/* and www.example.com/. Implementing this change allowed the videos to function immediately without any modifications to R2.
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.
