I've installed the Retour plugin and have been able to get most of my redirects working, but have now run into a situation where the old site had multi-level category hierarchy eg: /category/category-slug/sub-category-slug url structure and the new site only has a single level eg: /news/category/category-slug

How can I create a Regex to grab the last segment off the url, if there is more than one level of category, to use for my redirect ?

Looks like if there was only one level then the following would do:

/category/* to /news/category/$1

Any tips or ideas ?

1 Answer 1


Despite having used regex's for years, I still almost never get them right from memory. A great tool for testing them out is: https://regexr.com/

What you'll need to use is a capture group. Something like this for the source:


And then the resulting URI would be something like this:


Each set of () is a capture group; they are numbered sequentially, so $1 will output the first capture group.

If you go to https://regexr.com/ it will even explain every part of the regex, and what it does:

enter image description here

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.