1

I need to redirect some URLS to one domain and all other urls to another one. For example the redirect code which should redirect mydomain.com/?p=221 and mydomain.com/?p=222 to Google and all other to another domain.

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !\.(css|png|gif|jpe?g|js)$ [NC]
RewriteCond %{REQUEST_URI} !(?p=221|?p=222)& [NC]
RewriteRule ^(.*)$ http://mydomain.com/$1 [L,R=301]

RewriteCond %{REQUEST_URI} (?p=221|?p=222)& [NC]
RewriteRule ^(.*)$ http://google.com [L,R=301]

But the code doesn't work. Why is that?

4

1 回答 1

2

The query is not in the variable REQUEST_URI. You have to use the QUERY_STRING variable. Replace both conditions with these ones, respectively:

RewriteCond %{QUERY_STRING} !p=(221|222) [NC]
# And 
RewriteCond %{QUERY_STRING} p=(221|222) [NC]
于 2013-05-29T03:48:58.983 回答