I'm trying to redirect (or 'rewrite') over 3000 urls for a site where we updated the SEF of most of the content pages to remove an ID number that was not needed/desired. This should be a simple process using regex; however, I'm learning that regex and htaccess are more 'art' than science :-(
Here's the rule I have:
RewriteRule ^topics\/([-0-9a-zA-Z]+)?\/([0-9]+)(-)([0-9a-z,-]+)? http://example.net/topics/$1/$4 [L,R=301]
Most of the URLs are like this:
http://example.net/topics/management/6309-investing-proceeds-from-sale-of-a-farm-or-ranch
and work fine with the regex; however, if an article starts with a number, thus having a URL like this:
http://example.net/topics/management/3542-9-new-years-resolutions-for-cattle-producers
Then the regex (above) is grabbing more than just the first set of 4 ID digits and is also grabbing that first number (in this case "9") which should be left, resulting in this:
http://example.net/topics/management/new-years-resolutions-for-cattle-producers
Which obviously doesn't work.
In addition, as a work-around, I have tried creating some custom rules to just handle the few pages/URLS that have this format (with a number at the start of the article title), with a rule ahead of the regex rule like this:
Redirect 301 /topics/management/3542-9-new-years-resolutions-for-cattle-producers http://example.net/topics/management/9-new-years-resolutions-for-cattle-producers
or
RewriteRule ^topics\/([-0-9a-zA-Z]+)?\/([0-9]{1,4}?)(-)([0-9,a-z,-]+)? http://example.net/topics/$1/$4 [L,R=301]
However, when I do that, the regex rule (further down the htaccess file) still runs resulting in the "9" getting removed.
I have tested this on many different regex and htaccess test sites and they all work; however, it continues to fail on the live server.
The web server is: Apache/2.2.25 (Unix) mod_hive/4.0 mod_ssl/2.2.25 OpenSSL/1.0.0-fips mod_bwlimited/1.4 mod_fcgid/2.3.6
And I have already contact my server/hosting company and they say they don't have the expertise to be able to resolve this issue.
Can anyone see where the problem is here? I've searched through hundreds of forum posts here and other places with nobody having quite the same problem.
example.com
,example.net
andexample.org
as those are guaranteed to not be registered to anyone real. (They are specifically reserved for example/documentation purposes.)