toadly
toadly

Reputation: 343

.htaccess redirect for multiple IP ranges

I am trying to redirect multiple different IP addresses except those that match a specific user-agent. So far I've been able to successfully do this with 1 IP range, eg:

RewriteCond %{REMOTE_ADDR} ^123\.123\.123\.[0-9]{1,3}$
RewriteCond %{HTTP_USER_AGENT} !^useragent$
RewriteRule ^(.*)$ http://www.redirecthere.com/$1 [L]

So the above redirects the IPs 123.123.123.x to the URL - unless that IP has this useragent, in which case no redirect takes place.

I actually need to redirect several other IP ranges as well as this. But doing so as follows breaks everything:

RewriteEngine on
RewriteCond %{REMOTE_ADDR} ^123\.123\.123\.[0-9]{1,3}$
RewriteCond %{REMOTE_ADDR} ^124\.124\.124\.[0-9]{1,3}$
RewriteCond %{HTTP_USER_AGENT} !^useragent$
RewriteRule ^(.*)$ http://www.redirecthere.com/$1 [L]

What am I doing wrong? I wasn't able to find any duplicate questions.

Upvotes: 1

Views: 3344

Answers (1)

Croises
Croises

Reputation: 18671

You just need to add [OR] because the normal test is "AND":

RewriteEngine on
RewriteCond %{REMOTE_ADDR} ^123\.123\.123\.[0-9]{1,3}$ [OR]
RewriteCond %{REMOTE_ADDR} ^125\.125\.125\.[0-9]{1,3}$ [OR]
#.....
RewriteCond %{REMOTE_ADDR} ^124\.124\.124\.[0-9]{1,3}$
RewriteCond %{HTTP_USER_AGENT} !^useragent$
RewriteRule ^(.*)$ http://www.redirecthere.com/$1 [L]

Upvotes: 2

Related Questions