0

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 ^/(.*) http://www.google.com [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.
Vinay
  • 1
  • Phil - That is exactly what I want though. Or am I missing something? –  Jan 11 '11 at 12:34

2 Answers2

0

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

Phil P
  • 3,040
  • 1
  • 15
  • 19
0
RewriteCond %{REMOTE_ADDR} ^127\.0\.0\.1$
RewriteCond %{REQUEST_URI} !/StringOne [NC, OR]
RewriteCond %{REQUEST_URI} !/StringTwo [NC]
RewriteRule ^/(.*) http://www.google.com [R=302,L]

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

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

And I got this in the log:

127.0.0.1 - - [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 ^/(.*) http://www.google.com [R=302,L]
toppledwagon
  • 4,215
  • 24
  • 15