htaccess的重定向规则去除文章编号重定向、规则、编号、文章

2023-09-02 01:06:53 作者:深情不如久伴

我想重定向以下网址:

发件人: http://www.example.com/13-articlename TO: http://www.example.com/articlename

和我有以下code:

 的RewriteCond%{QUERY_STRING} ID = 13
重写规则(。*)$ http://www.example.com/articlename [R = 301,L]
 
301重定向代码写在哪

解决方案

在你重写你期待 ID的查询参数然而,在你的榜样,它实际上是部分该网址。

  RewriteEngine叙述上
的RewriteBase /
重写规则( d +) - ([^ /] *)$ 2 [R = 301,L]
 

( d +) =匹配任何数字 - =一个连字符 ([^ /] *) =,除了正斜杠的任何字符 $ 2 =重定向到第二个匹配组 - ([^ /] *) [R = 301] =使用HTTP 301重定向(忽略,如果你想拥有它改写,而不是重定向) [L] =最后一个规则(不处理下列规则)

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

测试

 输入网址
http://www.example.com/13-articlename

输出网址
http://www.example.com/articlename

调试信息
1 RewriteEngine叙述上
2的RewriteBase /
3重写规则( d +) - ([^ /] *)$ 2 [R = 301,L]
这条规则是满足,新的URL http://www.example.com/articlename
测试被停止,因为在你的重写规则选项R的。
一个重定向将与状态code 301进行
 

I want to redirect the following URLs:

From: http://www.example.com/13-articlename TO: http://www.example.com/articlename

And I have following code:

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

解决方案

In your rewrite you are expecting a querystring parameter of id however in your example it is actually part of the URL.

RewriteEngine on
RewriteBase /
RewriteRule (d+)-([^/]*) $2 [R=301,L]

(d+) = match any digits - = a hyphen ([^/]*) = any characters except a forward slash $2 = redirect to the second matching group - ([^/]*) [R=301] = use a HTTP 301 redirect (omit if you want to have it rewrite instead of redirect) [L] = Last rule (don't process following rules)

You can test at 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