+4 votes
in Web & Google by (17.9k points)

I moved my website to a different CMS. The old CMS used to generate URLs in the following format:

example.com/discussion/100/how-are-you

After discussion, it can have any number; I have used 100 as an example.

The new CMS generates URLs in the following format:

example.com/how-are-you

So, basically, I need to check if the URL contains "discussion/number", remove "discussion/number" from the URL, and redirect it.

Redirect from

example.com/discussion/100/how-are-you

to

example.com/how-are-you

How can I do it using .htaccess? 

1 Answer

+1 vote
by (48.9k points)

You can use the following htaccess code to remove the word "discussion" and number from the URL and redirect it to the new URL.

RedirectMatch 301 ^/discussion/([0-9]+)/(.*)$ example.com/$2

In the above code, if the URL contains "discussion/any number", "discussion/any number" is stripped and it is permanently redirected to the new URL. The new URL does not contain "discussion/any number".

In that above code, $1 represents string matching ([0-9]+) i.e. any number. $2 represent string matching (.*)$ i.e.anything that comes after the number.


...