Making Page Rule wildcard non-greedy

Hey folks,

I’m wondering if there is a way to have a non-greedy wildcard in page rules?

For the sake of an example, I’d like to redirect test.v4.wtf/*/* to redirect.v4.wtf/$1/test/$2, where the first * matches one path segment, and the second * matches anything else. So, if I were to request test.v4.wtf/1/2/3, I’d get redirected to redirect.v4.wtf/1/test/2/3.

I’ve configured the page rule as above, but currently if I request test.v4.wtf/1/2/3 it gets redirected over to redirect.v4.wtf/1/2/test/3.

Is there a way to have a non-greedy wildcard in Page Rules? This feels like a super basic thing but I can’t figure it out :frowning:

Cheers!

Each * corresponds to a $.

test.v4.wtf/*/* → the last * matches even something after like /3/4/5/6/index.html.

Would be test.v4.wtf/*/*/* → forward to redirect.v4.wtf./$1/$2/test/$3/.

You do have both cases, like /1/2/ and also combination like /1/2/3/ and maybe more segments? :thinking:

Either try to split it and use multiple Page Rules to catch that cases (just make sure to keep in mind the priority & order of the Page Rules) or, I might not be sure if something could be done with Transform Rules if that so.

And the amout of available Page Rules for use is related to the Plan you’re using.
Except, if we need we can purchase more Page Rules for $5 individually.

Also, Cloudflare Page Rules don’t support RegEx :confused:

Nevertheless, leaving here the below article for some clarification:

Patiently wait and maybe someone else might have the better answer.

I do not believe that is the case…

My page rule is defined as test.v4.wtf/*/*redirect.v4.wtf/$1/test/$2.
If I request test.v4.wtf/1/2/3, it is redirected to redirect.v4.wtf/1/2/test/3.

It seems that it is actually the first * that matches all the extra segments, not the last *. The behaviour that I desire would be for the last * to match extra segments.

The article you linked does seem to confirm this, the first * will be greedy (matching to the end of the URL), and then it back tracks from the end to match /*, so that second * only matches the final segment.

Rather annoying behaviour. Would be great to support something similar to RegEx’s lazy/non-greedy operator.

Unfortunately, Page Rules doesn’t support RegEx at any plan level.

If it’s not an extensive list of directories, you may try Bulk Redirects:
https://developers.cloudflare.com/rules/bulk-redirects/

Best to handle complex redirects at the server, or possibly with a Worker.

https://developers.cloudflare.com/workers/examples/bulk-redirects/

1 Like