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.

Sample:
site.com/xxxxxxxxxxxx
site.com/12uniquechar

When you visit the download page, a file or files will be downloaded with a direct link to:
site.com/direct/xxxxxxxxxxxx++xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
site.com/direct/12uniquechar++xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx

The 12 characters after site.com/direct/ 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 (site.com/12uniquechar).

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: