htaccess正则表达式需要帮助

时间:2013-12-24 20:43:19

标签: regex .htaccess

我在最好的时候不是很擅长正则表达式,现在我遇到了一个让我头疼的.htaccess文件的问题。这里的任何人都可以帮我写出正确的命令吗?

我尝试将RedirectMatch永久保存为具有如下图案的网址:

http://www.mydomain.tld/topic000-my-name-slug.html

其中000表示1,2或3位数。

我想将这些网址重写为以下模式:

http://www.mydomain.tld/my-name-slug

短语" my-name-slug"可以改变长度,并始终用连字符分隔单词。我不再想要.html了。

任何人都可以帮我吗?

3 个答案:

答案 0 :(得分:1)

试试这个:

RedirectMatch 301 topic[0-9]{1,3}-([\w-]+).html /$1

答案 1 :(得分:0)

尝试:

RedirectMatch 301 ^/[^-]*?[0-9]+-(.+)\.html$ /$1

或者:

RedirectMatch 301 ^/topic[0-9]+-(.+)\.html$ /$1

如果原始网址字面上以“主题”开头。

答案 2 :(得分:0)

这应该有效:

RewriteRule ^/topic[0-9]+-(.+)\.html$ /$1/ [R=301,L]