Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am trying to write a simple rewrite rule using the rewriteengine in apache. I want to redirect all traffic destined to a website unless the traffic originates from a specific IP address and the URI contains two specific strings.

RewriteEngine On
RewriteLog /var/log/apache2/rewrite_kudithipudi.log
RewriteLogLevel 1

RewriteCond %{REMOTE_ADDR} ^199\.27\.130\.105
RewriteCond %{REQUEST_URI} !/StringOne [NC, OR]
RewriteCond %{REQUEST_URI} !/StringTwo [NC]
RewriteRule ^/(.*) [R=302,L]

I put these statements in my virtual host configuration. But the rewriteengine seems to be redirect all requests, whether they match the condition or not. Am I missing something? Thank you.

  • Vinay.
share|improve this question
Phil - That is exactly what I want though. Or am I missing something? – user66388 Jan 11 '11 at 12:34

You've made the rewrite conditional upon coming from that IP address, not upon not coming from that IP address.

share|improve this answer
RewriteCond %{REMOTE_ADDR} ^127\.0\.0\.1$
RewriteCond %{REQUEST_URI} !/StringOne [NC, OR]
RewriteCond %{REQUEST_URI} !/StringTwo [NC]
RewriteRule ^/(.*) [R=302,L]

I put this into my apache config and set a RewriteLogLevel of 9 and did this request:

> telnet localhost 80
Connected to localhost.localdomain (
Escape character is '^]'.
GET /StringOne/StringTwo

And I got this in the log: - - [11/Jan/2011:00:02:34 --0800] [<host>/sid#2b1603885d30][rid#2b1603e75af0/initial] (4) RewriteCond: input='/StringOne/StringTwo' pattern='!/StringOne' [NC] => not-matched

It appears that apache evaluates the [NC] before it applies the [OR].

I think you want something closer to this:

RewriteCond %{REMOTE_ADDR} !^199\.27\.130\.105$
RewriteCond %{REQUEST_URI} !/StringOne [NC]
RewriteCond %{REQUEST_URI} !/StringTwo [NC]
RewriteRule ^/(.*) [R=302,L]
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.