Regex matching in Redirect Rule not behaving as expected

Attempting to use CF Redirect Rules to handle some legacy paths in a webapp.

I need to be able to match

  • /direct/323847/locations
  • /direct/323847/locations/167591
  • /direct/323847/locations/167591/checkout

the regex I have used in other contexts looks like


However, this isn’t invoking a redirect as I would expect, and when I look at the the expression preview, there is some kind of double escape thing happening

…http.request.uri.path matches “\/direct\/\d+\/locations(?:\/\d+)?(?:\/checkout)?”)

Anyone know what’s going on here?


That should be what you’re looking for. I’ve tried it against your examples.

\d also works if you edit the expression directly instead of using the expression builder:

(http.request.uri.path matches "/direct/\d+/locations(?:/\d+)?(?:/checkout)?"
1 Like

That works! Thank you so much.

Is there a reason that \d+ doesn’t work in the gui? There aren’t any super useful guides I could find to explain what will and won’t work, so I have just been testing different patterns and curling…

Something just seems weird about how things are escaped with these regexes…

Also, anything I should be looking out for here re weirdness around capture groups?

I am going to want to capture the numbers in these paths such that they can be used in the forwarding path.

You’ll want to use the regex_replace function for that:

You don’t need to capture in the filter expression, only in the redirect target.

It would probably work with a double escape or something, but I haven’t bothered trying. Too many escapes just make the expression unreadable.

Agree on readability. Curious what your testing strategy is for these regexes/redirects. I have just been restricting the host/ip and curling things until they work. Might write some sort of script/tests so that I can feel more confident.

I hadn’t actually looked at your expression before, just removed the escapes and replaced \d with [0-9].

I just had a closer look at it, and don’t believe it matches the examples you gave above.
It also matched /direct/323847/locations/checkout

So this regex should do what you’re looking for:


Try for example here:
A few examples is usually how I’d test a regex.

However, based on how you are looking do redirect this, it’s probably better to go with distinct rules for your 3 cases. The resulting regex_replace call would get really, really ugly depending on how you want to build the new target url.

Thanks! I agree that doing 3 distinct rules is probably a good bet.

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