Jeff Jeff - 2 months ago 8
PHP Question

htaccess Page Redirect to URL with no page ID

This URL

http://www.example.com/news/2138-gideon-rothschild-listed-leading-lawyer-expert-guides

needs to be redirected to this URL

http://www.example.com/news/gideon-rothschild-listed-leading-lawyer-expert-guides.

Here is another example:

http://www.example.com/articles/2140-can-you-trust-your-trust

to

http://www.example.com/articles/can-you-trust-your-trust

The client wants the page id removed from the URL. Can this redirect be done in the htaccess file or do I need to do it using PHP?

Answer

Yes, it can be done using a RedirectMatch rule.

Place this rule in your site root .htaccess:

RedirectMatch 301 ^/(articles)/\d+-(.+)$ /$1/$2