Rewrite wildcard domain to subdirectories in R2 bucket


(please read DOMAIN as a valid domain name - no rights to post a domainname)

I’d like to rewrite wildcard subdomains in the CF dns to subdirectories on R2.
Desired result:
will be served from:

This works more or less on old school apache .htaccess:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(.)
RewriteRule ^(.
)$ DOMAIN/%1/$1 [P,NC,QSA]

Already found the 301/302 redirect with CF page rules, but this reveals the hosting url AFAIK (bad for SEO and user experience).

Trying rules → transform rules with
( eq “*.DOMAIN”) static rewrite to R2DOMAIN/$1
but this give an Error 1016

Maybe I need two steps? One to redirect the subdomain to a subdirectory on the same domain and then connect the subdirectory to the R2 subdirectory?
And what need to be in the DNS? Now a wildcard cname *

Any help is welcome.
Thanks in advance.

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