htaccess正则expression式不正确处理URL

我试图redirect(或“重写”)超过3000个url,我们更新了大部分内容页面的SEF,以删除不需要或不需要的ID号码。 这应该是一个简单的过程使用正则expression式; 然而,我正在学习正则expression式和htaccess比科学更“艺术”:-(

这是我有的规则:

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

大部分url都是这样的:

 http://example.net/topics/management/6309-investing-proceeds-from-sale-of-a-farm-or-ranch 

并与正则expression式工作正常; 但是,如果一篇文章以一个数字开头,那么这样的URL就是这样的:

 http://example.net/topics/management/3542-9-new-years-resolutions-for-cattle-producers 

然后,正则expression式(上面)抓取的不仅仅是第一组4位ID数字,还抓住了应该留下的第一个数字(本例中为“9”),导致:

 http://example.net/topics/management/new-years-resolutions-for-cattle-producers 

这显然不起作用。

另外,作为一个解决方法,我已经尝试创build一些自定义规则来处理这个格式的less数页面/ URL(在文章标题的开始处有一个数字),并且规则在正则expression式之前这个:

 Redirect 301 /topics/management/3542-9-new-years-resolutions-for-cattle-producers http://example.net/topics/management/9-new-years-resolutions-for-cattle-producers 

要么

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

但是,当我这样做时,正则expression式规则(进一步向下的htaccess文件)仍然运行导致“9”被删除。

我已经testing了许多不同的正则expression式和htaccesstesting网站,他们都工作; 但是,它仍然在现场服务器上失败。

Web服务器是: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

而且我已经联系了我的服务器/托pipe公司,他们说他们没有专业知识能够解决这个问题。

任何人都可以看到问题在这里? 我在这里和其他地方search了数百个论坛post,没有任何人有相同的问题。

我只是尝试一下你的configuration一个小的改变。 我正在configuration文件而不是.htaccess使用这个(注意它search开始/主题 – 而不是主题):

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

然后testing按预期工作:

 [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:

请试试这个:

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

在第一个短划线之前用(\d+)replace数字,而不是在(\d+)之前使用([0-9]+) ,在我的设置中仍然有效,也许在你的(\d+)中感觉你的apache编译更加舒适。