.htaccess 使用mod rewrite重定向,使用替换字符的查询字符串值

.htaccess 使用mod rewrite重定向,使用替换字符的查询字符串值,.htaccess,mod-rewrite,.htaccess,Mod Rewrite,我有这样的旧URL: http://www.foo.org/cgi-bin/search.cgi?Blog=14&tag=my%20long%20tag%20name&limit=100 我想重定向到另一台服务器的页面,如: http://www.bar.org/tag/my-long-tag-name/ 在foo.org上,我有: RewriteCond %{QUERY_STRING} ^Blog=14&tag=([^&]*)&.*$ [NC] Re

我有这样的旧URL:

http://www.foo.org/cgi-bin/search.cgi?Blog=14&tag=my%20long%20tag%20name&limit=100
我想重定向到另一台服务器的页面,如:

http://www.bar.org/tag/my-long-tag-name/
在foo.org上,我有:

RewriteCond %{QUERY_STRING} ^Blog=14&tag=([^&]*)&.*$ [NC]
RewriteRule ^cgi-bin/search.cgi$ http://www.bar.org/tag/%1/?  [NC,R=301,L]
这将重定向到bar.org上的正确页面,在那里我需要用连字符替换
%20
s。但是,它会将每个
%20
更改为
%2520
。我已尝试添加
NE
标志,但没有更改这是我一直坚持的事情。

到达后,此规则将用连字符替换
%20
s:

RewriteRule ^blog/tag/([^\s]*)(?:\s)+(.*)/$ /blog/tag/$1-$2/ [R=301,L]

(奖励点…一些原始的
标签
值中也有
%28
%29
,我最终想删除它们。因此,
bob%20和%20%28的标签变成了
bob和thelma

您可以在网站根目录中使用这些规则。http://code>www.foo.org

RewriteEngine On

# temporary rewrite to /tag/tag-name
RewriteCond %{QUERY_STRING} ^Blog=14&tag=([^&]*) [NC]
RewriteRule ^cgi-bin/search\.cgi$ /tag/%1? [L]

# redirect to http://www.bar.org if there is no special char in URI
RewriteRule ^tag/[^\x20\x28\x29]+$ http://www.bar.org/$0 [NC,L,NE,R=301]

# if special char is in the end then simple remove it
RewriteRule ^(tag)/([^\x20\x28\x29]*)[\x20\x28\x29]+$ $1/$2 [NC,N,DPI]

# otherwise replace special char by a hyphen    
RewriteRule ^(tag)/([^\x20\x28\x29]*)[\x20\x28\x29]+(.+)$ $1/$2-$3 [NC,N,DPI]

阿努巴瓦,太棒了!非常感谢你!我只是想知道这是怎么回事,但我永远也不可能做到。有一件事-在第4行中,
标记/%1?
之前应该有一个
/
。再次感谢。事实上,我在测试中已经重写了
RewriteBase/
,这就是为什么我在
标记之前不需要
/
,但现在已经修改了