Code Bytes
Redirect old numbered URLs without breaking useful numbers
A tightly scoped Apache rule for a known legacy URL pattern, with checks for collisions, query strings and permanent redirect mistakes.
Removing an old article ID can make a URL easier to read. Removing every number from every URL is a different thing entirely: years, product models and version numbers may be meaningful.
Match the pattern you actually retired
Suppose /news/my-article-1234/ has moved to /articles/my-article/. On Apache 2.4, put this above the CMS rules in the site's root .htaccess, provided your host permits mod_rewrite there:
RewriteEngine On
RewriteCond %{REQUEST_METHOD} ^(?:GET|HEAD)$
RewriteRule ^news/([a-z][a-z0-9-]*)-([0-9]+)/$ https://example.com/articles/$1/ [R=302,L]
Replace example.com with the fixed destination hostname. The first capture is the slug; the second is the discarded ID. The destination is deliberately not taken from the incoming Host header. This example accepts simple lowercase slugs only, leaving unexpected paths alone.
Start with 302 while checking the mapping. Once every destination is correct, change it to 301 for the permanent move. Existing query strings are retained because this substitution does not introduce a new one; add QSD only if you have explicitly decided they should be discarded.
Check before making it permanent
Check a real matching URL, a slug containing a meaningful year, a nested path, a query string and an unrelated page. Confirm one redirect followed by a genuine destination page, not a second redirect or a soft 404.
If different numbered URLs would collapse onto the same slug, use an explicit redirect list instead. This rule is Apache configuration—not Nginx, Cloudflare or JavaScript—and should not be pasted into those systems.
References: Apache rewrite patterns and redirect/query flags.
Original version28 July 2016
Kept here for reference and earlier links. The updated guide above is the recommended starting point; older code may depend on retired services or different software versions.
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.
Keep exploring