2017-02-23 27 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