Rewrite URLs with numbers to new format

Posted by: Alex on July 28, 2016

A common URL structure or format is to include unique identifying numbers at the end of the URL. This used to be very common as certain organisations like Google News forced these uniquely identifiable integers for tracking.

Google News no longer requires these ugly numbers, and as such many people are deciding to ditch them as it’s a nicer URL and experience for users to just have the URL as short, simple and concise as possible.

For that reason I have created the following htaccess code that can be easily edited to rewrite from one number format to the raw word format without the numbers at the end:

RewriteRule ^([^/]+)-([0-9]+)/$ https://www.newsite.com/$1.htm [L,R=301]

This .htaccess first looks for any combination of letters at all ^([^/]+) followed by a hyphen - and then any combination of numbers ([0-9]+) and then the ending slash /. Then thanks to the way our regex works we simply use the first code section we surrounded in brackets ^([^/]+) as $1 in the URL rewrite. If you wanted access to the numbers still, they’d be accessible with $2.