Redirect from to

Found this amazing cheat sheet, but couldn’t figure out my use case unfortunately.

I want my visitors to to go to instead.

The last subdirectory abc can be dynamic. Basically I need to detect whether they’re targeting URL has parent instead of parents to avoid dead links.

What I’m doing right now is this:
URI Path equals /parent/
(http.request.uri.path eq "/parent/")

Static /parents/ 301 Preserve query string TRUE.

But what it does is obviously redirecting from to, losing the last subdirectory.

How do I write a redirect so that it preserves the last subdirectory?

Thank you!

Replacing a string within the path requires the function regex_replace(), available with Business Plan or higher.

When incoming requests match
Field: URI Path
Operator: starts with
Value: /parent/

Type: Dynamic
Expression: concat("", regex_replace(http.request.uri.path, "/parent/", "/parents/"))
Status code: 301
Preserve query string: yes

If your plan is Pro or Free, you can still use Page Rules, for as long as they are not deprecated:

Setting: Forwarding URL
Status: 301
Destination URL:$1

Make sure the Page Rule runs before any other Page Rule for the same path pattern.

1 Like

Wow! I’m on a free plan. Surprised that I stumbled upon such a narrow use case apparently, that it’s only solvable in the Business plan. Seems a bit of an overkill to be honest.

Thank you though, the Page Rules solution works wonderfully right now! I wonder for how long it will, haha.

1 Like

I’ve just found out that, as it turns out, you can actually use the substring() function, available in all plans, instead of regex_replace(). I’ve updated the Redirect Rules (beta) Cheat Sheet document.

In your case, you can try the following Redirect Rule:

When incoming request match...
URI Path starts with /parent/
Expression: concat("", substring(http.request.uri.path, 0,7), "s", substring(http.request.uri.path,7))

This is awesome, thanks so much!

1 Like

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