How do I use CF Rewrite URL to redirect a direct link?

I have this self-hosted temporary file hosting system that, after file upload, produces a download URL.


When you visit the download page, a file or files will be downloaded with a direct link to:

The 12 characters after are always the same 12 characters from the download page.

Now I want to forbid downloading the files directly and require that a user always use the download page.
Basically, I want to redirect the direct download link if it’s accessed directly or referred from another site to its own download page (

I already tried the rewrite rules, but I cannot figure out how to strip out the extra characters after the unique first 12 characters and also the /direct/ URI.

Thanks for the help.

Too bad “Regex support” is only for business plans.
I think it’s not possible on the pro and below plans.

1 Like

How do I do it via CF Worker? Has anyone tried a similar scenario?

I think this example is very close: