2017-08-03 24 views
1

我想將所有頁面請求重定向到另一個域,但排除一些我想單獨重定向的請求URI。所以我試過這樣的事情:如何從重定向中排除請求URI?

RewriteCond %{REQUEST_URI} !^/index.php?id=132$ [NC,OR] 
RewriteCond %{REQUEST_URI} !^/index.php?id=133$ [NC] 
RewriteRule .* https://new-domain.com/? [R=301,L] 

Redirect 301 /index.php?id=132 https://new-domain.com/example 
Redirect 301 /index.php?id=133 https://new-domain.com/example2 

但是,這不工作,有人可以告訴我什麼是錯的?

回答

1

%{REQUEST_URI}變量或Redirect指令與查詢字符串不匹配。

您可以用更好的正則表達式像這樣使用它:

RewriteCond %{THE_REQUEST} !/index\.php\?id=13[23][&\s] [NC] 
RewriteRule^https://new-domain.com/? [R=301,L] 

RewriteCond %{THE_REQUEST} /index\.php\?id=132[&\s] [NC] 
RewriteRule^https://new-domain.com/example? [L,R=301] 

RewriteCond %{THE_REQUEST} /index\.php\?id=133[&\s] [NC] 
RewriteRule^https://new-domain.com/example2? [L,R=301] 

還要注意使用的?在目標URL拋棄以前的查詢字符串。

+1

作品像一個魅力,謝謝:) – Fox

+0

但你能告訴我爲什麼它不起作用,如果我添加一些其他條件,如: RewriteCond%{THE_REQUEST}!/ index \ .php \?id = 13 [ [NC,OR] RewriteCond%{THE_REQUEST}!/ index \ .php \?id = 41 [&\ s] [NC] ... 看起來第一個條件是還在工作,但第二個條件是不工作:(。 – Fox

+0

由於這是否定,你需要從你的條件'或'。 – anubhava