[Page Rules] Set redirect with exceptions

I am facing following issue. I want to use Cloudflare Page Rules to get something like this below:

example.com
(1) / (domain root) => should not redirect
(2) /subfolder => should not redirect
(3) /$1 => should redirect to example.com/xyz/$1

So, anything after the “/” should be redirected to /xyz/$1, but only if the user is not requesting the domain root or a subfolder of it.

Can anybody help?

Thank you.

I don’t think that’s going to work.

I was thinking of a string of Page Rules, but after ignoring root, then ignoring a subfolder, it will go into a redirect loop as it Matches /xyz/filename.php and then rewrites it as /xyz/filename.php/xyz/filename.php, which isn’t root or a subfolder.

Okay. Thank you.
Any other possibility to achieve this, maybe with htaccess or reverse proxy rules?

Probably htaccess so you can be more specific about the position of the wildcard, but I’m not a mod_rewrite expert.

Well mod_rewrite has the ‘-f’ and ‘-d’ options which can let you differentiate between files and folders so you could almost certainly do what you want using .htaccess or equiv on Apache.

If you want to use Page Rules you’d need to see if you can make the selection in rule (3) a bit more specific… ie. do those pages have an extension (rare these days, I know). You could do something like example.com/*.html → htttps://example.com/xya/$1.html, for example. You need to somehow get that selection more specific than just a string as Cloudflare has no idea if that string is accessing a folder or file, only your webserver would know that.

1 Like

This topic was automatically closed after 14 days. New replies are no longer allowed.