.htaccess重定向规则,用于删除文章ID

时间:2013-11-09 14:31:10

标签: .htaccess joomla

我想重定向以下网址:

来自: http://www.example.com/13-articlename
TO: http://www.example.com/articlename

我有以下代码:

RewriteCond %{QUERY_STRING} id=13
RewriteRule (.*)$ http://www.example.com/articlename [R=301,L]

1 个答案:

答案 0 :(得分:2)

在重写时,您期望查询字符串参数为id,但在您的示例中,它实际上是URL的一部分。

RewriteEngine on
RewriteBase /
RewriteRule (\d+)-([^/]*) $2 [R=301,L]
  • (\d+) =匹配任何数字
  • - =连字符
  • ([^/]*) =除正斜杠之外的任何字符
  • $2 =重定向到第二个匹配组 - ([^/]*)
  • [R=301] =使用HTTP 301重定向(如果您想重写而不是重定向,则省略)
  • [L] =上一条规则(不处理以下规则)

您可以在http://htaccess.madewithlove.be/

进行测试
input url
http://www.example.com/13-articlename

output url
http://www.example.com/articlename

debugging info
1 RewriteEngine on  
2 RewriteBase / 
3 RewriteRule (\d+)-([^/]*) $2 [R=301,L]
This rule was met, the new url is http://www.example.com/articlename 
Test are stopped, because of the R in your RewriteRule options. 
A redirect will be made with status code 301