2

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.

3
  • I suggest that you use example.com, example.net and example.org as those are guaranteed to not be registered to anyone real. (They are specifically reserved for example/documentation purposes.)
    – user
    Apr 15, 2015 at 14:07
  • Michael - thanks, sorry I wasn't thinking about that when I posted. Thanks for correcting that post error on my part. Apr 15, 2015 at 14:20
  • Well, after trying many different sytnax we got one to work for a specific folder (i.e. 'topics') then used the same one on another folder ('news') and it didn't work. We've also tested on several different servers and different results. Unfortunately, we've come to the conclusion that any sort of 'more than basic' regex rule within htaccess is not reliable (at least with the version of Apache that we have - and I haven't seen any release notes/reports that indicate that Apache 2.4 does anything to aid in this area). SO ... we've decided that we'll need to create 3000+ manual entries :-( Apr 17, 2015 at 20:53

1 Answer 1

0

I've just try your setup with a small change respect to your config. I am configuring it on the config file instead of .htaccess using this (note it searches for beggining with /topics -instead of topics-):

RewriteRule ^/topics\/([-0-9a-zA-Z]+)?\/([0-9]+)(-)([0-9a-z,-]+)? http://example.net/topics/$1/$4 [L,R=301]

Then the test works as expected:

[root@proxy conf]# curl -i http://localhost/topics/management/3542-9-new-years-resolutions-for-cattle-producers
HTTP/1.1 301 Moved Permanently
Date: Wed, 15 Apr 2015 14:48:09 GMT
Server: Apache
Location: http://example.net/topics/management/9-new-years-resolutions-for-cattle-producers
Content-Length: 289
Content-Type: text/html; charset=iso-8859-1

EDIT1:

Please try this one:

RewriteRule ^topics\/([-0-9a-zA-Z]+)?\/(\d+)(-)([0-9a-z,-]+)? http://example.net/topics/$1/$4 [L,R=301]

Instead of use ([0-9]+) for the numbers before the first dash its replaced by (\d+), on my setup still works, perhaps in yours the (\d+) makes feel your apache compilation more confortable.

5
  • Well, on our server that actually broke the rule completely (didn't match at all). I also attempted other 'variations' (escaping the "/"; with/without the "^"; etc.). Still doesn't work. Also, I've had other people test this on local set-ups and also have it work, but still not work on my hosting company's account. Is there any sort of doc describing exactly what regex commands are supported by which version of apache? Apr 15, 2015 at 20:44
  • Then seems like the apache installed was compiled with a buggie regex library :(. Perhaps you can ask our hosting company to upgrade the apache ... Apr 16, 2015 at 5:32
  • @ChrisPaschen I changed just a bit your regex, let's see if it helps :) Apr 16, 2015 at 8:02
  • thanks for the suggestion; however, that doesn't match at all now. I'm very seriously wondering if the apache is buggy; however, I doubt that my hosting company will admit that. I'm considering asking them to upgrade to Apache 2.4 - not sure if that will help any (or cause other problems). Apr 16, 2015 at 15:12
  • CORRECTION (I forgot to clear the browser cache before I tested) - it does work, but results in the same problem (the initial digit of the file name being removed) Apr 16, 2015 at 15:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .