Redirect from site.com/parent/A to site.com/parentS/A

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

I want my visitors to https://sub.site.com/parent/abc to go to https://sub.site.com/parentS/abc 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/")

Then
Static /parents/ 301 Preserve query string TRUE.

But what it does is obviously redirecting from sub.site.com/parent/abc to sub.site.com/parents, 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/

Then
Type: Dynamic
Expression: concat("https://www.example.com", 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:

URL: https://www.example.com/parent/*
Setting: Forwarding URL
Status: 301
Destination URL: https://www.example.com/parents/$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/
Then
Dynamic
Expression: concat("https://examle.com", substring(http.request.uri.path, 0,7), "s", substring(http.request.uri.path,7))
2 Likes

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.