2017-02-23 25 views
1

我有旧网址是这样的:使用国防部重写重定向,使用查询字符串值与字符替换

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用连字符。但是,它每%20更改为%2520。我试过添加NE的标志,但没有改变。 这是我坚持的东西。

一旦出现,这个规则取代%20 s的连字符:

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

(加分......其中一些原始tag值也有%28和他们%29,最终我想。删除所以bob%20and%20%28thelma%29标签变得bob-and-thelma

回答

1

您可以使用的www.foo.org网站根目录的.htaccess这些规则:

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] 
+1

anubhava真是太棒了!非常感谢!我只是了解这是如何工作,但永远不会到达那里。有一件事 - 在第4行中,'tag /%1?'之前应该有'/'。再次感谢。 –

+0

其实我在测试中有'RewriteBase /',这就是为什么我不需要''''tag'之前'但是现在修改了 – anubhava