Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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.
share|improve this question
    
Phil - That is exactly what I want though. Or am I missing something? –  user66388 Jan 11 '11 at 12:34

2 Answers 2

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 ^/(.*) 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]
share|improve this answer

Your Answer

 
discard

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.