<p>Despite having used regex's for years, I still almost never get them right from memory. A great tool for testing them out is: <a href="https://regexr.com/" rel="nofollow noreferrer">https://regexr.com/</a></p>

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

<p><code>/category/(.*$)</code></p>

<p>And then the resulting URI would be something like this:</p>

<p><code>/news/category/$1</code></p>

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

<p>If you go to <a href="https://regexr.com/" rel="nofollow noreferrer">https://regexr.com/</a> it will even explain every part of the regex, and what it does:</p>

<p><a href="https://i.sstatic.net/aaKBV.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/aaKBV.png" alt="enter image description here"></a></p>

CC BY-SA 3.0
2018-01-23T13:19:17.677Z
2018-01-23T13:19:17.677Z
andrew.welch
<p>I've installed the <strong>Retour</strong> 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</p>

<p>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 ? </p>

<p>Looks like if there was only one level then the following would do: </p>

<p><strong>/category/* to /news/category/$1</strong> </p>

<p>Any tips or ideas ? </p>

<redirect><plugin-retour>
How do I redirect from a legacy category URL to a new catgory URL using Retour
# How do I redirect from a legacy category URL to a new catgory URL using Retour

(Asked by: username_0 on 2018-01-22)

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 ?

**Tags:** redirect, plugin-retour

---

## 1 answers

### Answer by username_1 on 2018-01-23. Score: 3 | Accepted answer

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:

`/category/(.*$)`

And then the resulting URI would be something like this:

`/news/category/$1`

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](https://i.sstatic.net/aaKBV.png)](https://i.sstatic.net/aaKBV.png)


---

