3

I rarely need to do redirects and when I do they're usually simple. This time I'm trying to do something a little more advanced and I'm stumped. Regex is hard :)

This is a WordPress site and I'm using the Redirection plugin by John Godley.

I have a bunch of posts grouped by districts. I need to redirect both the landing pages for those districts:

(landing page) 

foo.com/listing/old-district/ -> foo.com/new-district/

And all posts within those districts:

(posts in those districts)

foo.com/listing/old-district/post1 -> foo.com/new-district/post1
foo.com/listing/old-district/post2 -> foo.com/new-district/post2
etc

Any suggestions?

1 Answer 1

2

This is a WordPress site and I'm using the Redirection plugin by John Godley.

There is an example on the Plugin's "Redirect Regular Expressions" page that appears to do exactly what you need.

For example, your "source URL" would be:

/listing/old-district/(.*)

and corresponding "Target URL" would be:

/new-district/$1

The pattern (.*) captures anything (including nothing) that might follow /listing/old-district/. $1 in the target URL is a backreference that contains whatever has been captured from the "source URL" (if anything).

In more detail, the . (dot) matches any character (except newlines) and the * is a quantifier that states we should match 0 or more of the preceding pattern (in this case, just a single character).

1
  • 1
    Thanks MrWhite, as it turns out I was getting mighty close to just that :) I'll give that a try and let you know how it goes. Commented Jan 3, 2020 at 21:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.