Apache on our CentOS VDS is being monitored by the hosting company (due to previous issues when on a shared hosting server). The site they are monitoring is an on-line store and this has the rather unfortunate side effect of making it appear as if there are numerous visitors on-line at any given moment. It also fills the store's logs with irrelevant entries. None of this is critical but I'd like to eliminate these issues.

I am trying to use mod_rewrite commands in the .htaccess file to redirect that one IP address to a dummy page rather than letting it enter the store. Googling produces many examples but they are all for requests for a specific page, whereas in my case no page is being requested.

Here is what I've ended up with (192.168.0.23 being my test client) but it just results in a loop:

RewriteCond %{REMOTE_HOST} 192\.168\.0\.23
RewriteRule .* /monitor.html [R=301,L]

This example is being tested on my dev server using wget (using either the IP address or the domain name) and results in this:

H:\>wget http://192.168.0.18
--2011-09-26 09:01:07--  http://192.168.0.18/monitor.html
Connecting to 192.168.0.18:80... connected.

the response is repeated until

--2011-09-26 09:01:07--  http://192.168.0.18/monitor.html
Connecting to 192.168.0.18:80... connected.
HTTP request sent, awaiting response... 301 Moved Permanently
Location: http://192.168.0.18/monitor.html [following]
20 redirections exceeded.

Is what I'm trying to do possible using mod_rewrite or should I tackle this some other way?

tl;dr:

  • I need to redirect any request from a specific IP address to a dummy page
  • The response must come from the web server, as that is what is being monitored

Edit

Here is the content of rewrite_log after performing another wget http://192.168.0.18

192.168.0.23 - - [21/Sep/2011:21:27:07 +1000] [192.168.0.18/sid#937eab8][rid#95330f8/initial] (2) [perdir /path/to/website/files/] explicitly forcing redirect with http://192.168.0.18/monitor.html
192.168.0.23 - - [21/Sep/2011:21:27:07 +1000] [192.168.0.18/sid#937eab8][rid#95330f8/initial] (1) [perdir /path/to/website/files/] escaping http://192.168.0.18/monitor.html for redirect
192.168.0.23 - - [21/Sep/2011:21:27:07 +1000] [192.168.0.18/sid#937eab8][rid#95330f8/initial] (1) [perdir /path/to/website/files/] redirect to http://192.168.0.18/monitor.html [REDIRECT/301]
192.168.0.23 - - [21/Sep/2011:21:27:07 +1000] [192.168.0.18/sid#937eab8][rid#9535100/initial] (3) [perdir /path/to/website/files/] strip per-dir prefix: /path/to/website/files/monitor.html -> monitor.html
192.168.0.23 - - [21/Sep/2011:21:27:07 +1000] [192.168.0.18/sid#937eab8][rid#9535100/initial] (3) [perdir /path/to/website/files/] applying pattern '^/monitor\.html$' to uri 'monitor.html'
192.168.0.23 - - [21/Sep/2011:21:27:07 +1000] [192.168.0.18/sid#937eab8][rid#9535100/initial] (4) [perdir /path/to/website/files/] RewriteCond: input='192.168.0.23' pattern='192\.168\.0\.23' => matched
192.168.0.23 - - [21/Sep/2011:21:27:07 +1000] [192.168.0.18/sid#937eab8][rid#9535100/initial] (2) [perdir /path/to/website/files/] rewrite 'monitor.html' -> '/monitor.html'
192.168.0.23 - - [21/Sep/2011:21:27:07 +1000] [192.168.0.18/sid#937eab8][rid#9535100/initial] (2) [perdir /path/to/website/files/] explicitly forcing redirect with http://192.168.0.18/monitor.html
192.168.0.23 - - [21/Sep/2011:21:27:07 +1000] [192.168.0.18/sid#937eab8][rid#9535100/initial] (1) [perdir /path/to/website/files/] escaping http://192.168.0.18/monitor.html for redirect
192.168.0.23 - - [21/Sep/2011:21:27:07 +1000] [192.168.0.18/sid#937eab8][rid#9535100/initial] (1) [perdir /path/to/website/files/] redirect to http://192.168.0.18/monitor.html [REDIRECT/301]

Hopfuly someone can make sense of it. As far as I can tell it thinks monitor.html <> monitor.html.

link|improve this question

feedback

2 Answers

up vote 2 down vote accepted
RewriteEngine On
RewriteCond %{REMOTE_ADDR} 192\.168\.0\.23
RewriteCond %{REQUEST_URI} !/monitor\.html$
RewriteRule $ /monitor.html [R=301,L]

~

link|improve this answer
Bingo! (filler to satisfy the 15 character minimum) – John Gardeniers Sep 26 '11 at 2:05
Thank You, John :) – SparX Sep 26 '11 at 2:08
Oops. I was too quick on the trigger. The redirect certainly works but it's redirecting every request to /monitor.html – John Gardeniers Sep 26 '11 at 2:09
Double oops. I tested by removing the "3" from the address but of course 192\.168\.0\.2 still matches 192.168.0.23. I need to slow down and concentrate more. :( – John Gardeniers Sep 26 '11 at 2:11
:( Something to do with the Condition. Let me double check. – SparX Sep 26 '11 at 2:12
show 5 more comments
feedback

Just don't redirect when the requested page is the monitoring page:

RewriteCond %{REMOTE_HOST} ^192\.168\.0\.23$
RewriteRule !^monitor\.html$ /monitor.html [R=301,L]

(edited for .htaccess context and exact host IP matching)

link|improve this answer
I don't understand why but I'm still getting a redirection look. Sure looks like it should work. – John Gardeniers Sep 26 '11 at 0:30
Try using REMOTE_ADDR instead of REMOTE_HOST. – Steven Monday Sep 26 '11 at 1:25
@JohnGardeniers I've got nothing. Maybe set a RewriteLog and RewriteLogLevel 9, see if that gives us anything interesting? – Shane Madden Sep 26 '11 at 1:27
Question updated. – John Gardeniers Sep 26 '11 at 1:59
@JohnGardeniers Oh, it's in .htaccess, missed that. Just drop the / from the match. RewriteRule !^monitor\.html$ /monitor.html [R=301,L] – Shane Madden Sep 26 '11 at 2:10
feedback

Your Answer

 
or
required, but never shown

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