Nginx – Remove id or number from url using nginx rewrite

nginxrewrite

I have a website which was running on custom CSM. However, as it grew larger we had to move it to wordpress.

On the old CMS we had url structure to articles similar to this:

https://example.com/parent-category/child-category/7564-title-of-the-article
https://example.com/parent-category
https://example.com/parent-category/child-category

We would like to redirect all requests to old articles to this structure

https://example.com/title-of-the-article
https://example.com/category/parent-category
https://example.com/category/parent-category/child-category

Here category is wordpress slug for category

I did try some redirect rules in nginx, however, it turns out I am not good with regex expressions. I am clueless. Please help me do it. I don't know where to turn to.

It would be highly appreciated.

Update: Edited original link structure to iron out confusions about "category" being a static slug. It is not.
There are multiple categories and sub categories:

https://example.com/review/app/734-title-of-the-post
https://example.com/news/546-title-of-the-post
https://example.com/blog/opinion/456-title-of-the-post

Best Answer

Assuming that "category" is literal and "something" is a placeholder, something like this might work for you:

rewrite ^/category/[^/]+/\d+-(.+)$ /$1 permanent;

If only old CMS URIs begin with /category, you could place the rewrite inside a location block to improve efficiency:

location ^~ /category/ {
    rewrite ^/category/[^/]+/\d+-(.+)$ /$1 permanent;
}

See this link for help with regular expressions.