2014-11-24 68 views
0

我有搜索webstie用戶在url中輸入以下查詢並執行搜索。.htaccess 302使用正則表達式替換重定向

http://www.example.com/?search=key word&type=sin 
http://www.example.com/?search=key word 

我做在頁面的規範鏈接,以避免這些在goodle被索引。

<link rel="canonical" href="http://www.example.com/key-word/"/> 

但是我需要添加一個302重定向以及對此。即

Redirect 302/http://www.example.com/key-word/ (key word is replaced spaces to hypen) 

我該怎麼做?

回答

1

您可以在DOCUMENT_ROOT/.htaccess文件中使用此代碼:

RewriteEngine On 

RewriteCond %{THE_REQUEST} \s/+(?:index\.php)?\?search=([^\s&]+) [NC] 
RewriteRule^/%1? [R=302,L,NE] 

## replace of space with - 

# executes **repeatedly** as long as there are more than 1 spaces in URI 
RewriteRule "^(\S*) +(\S* .*)$" /$1-$2 [L,NE] 

# executes when there is exactly 1 space in URI 
RewriteRule "^(\S*) (\S*)$" /$1-$2? [L,R=302,NE] 
+0

沒有工作似乎被重定向到相同的URL – mahen3d 2014-11-24 02:30:01

+0

有一個錯字,現在是固定的。 – anubhava 2014-11-24 02:38:59

+0

現在它重定向,但似乎添加像這樣的查詢字符串「xyz.com/key-word/?search=key word」 – mahen3d 2014-11-24 02:42:33