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

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


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

The new CMS generates URLs in the following format:


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

Redirect from




How can I do it using .htaccess? 

1 Answer

+1 vote
by (281k 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.