Redirecting users by IP in Apache
May 12, 2008 1:16 PM Subscribe
How do I redirect incoming website visitors to an external site by IP address?
I'm the general sysadmin here and don't do much web stuff other than simple content updating, but I think I'm on the right track with adding this bit of mod_rewrite code to the end of the httpd.conf file...
RewriteEngine on
RewriteCond %{REMOTE_ADDR} ^12\.345\.678\.9$
RewriteCond %{REQUEST_URI} ^/index.php$ [NC]
RewriteRule ^/(.*) http://test.foo.com/$1 [R]
Any ideas why this isn't working?
(apache 1.3.33 on OS X)
I'm the general sysadmin here and don't do much web stuff other than simple content updating, but I think I'm on the right track with adding this bit of mod_rewrite code to the end of the httpd.conf file...
RewriteEngine on
RewriteCond %{REMOTE_ADDR} ^12\.345\.678\.9$
RewriteCond %{REQUEST_URI} ^/index.php$ [NC]
RewriteRule ^/(.*) http://test.foo.com/$1 [R]
Any ideas why this isn't working?
(apache 1.3.33 on OS X)
RewriteEngine On
RewriteCond %{REMOTE_ADDR} ^71\.5\.1\.212$
RewriteRule .* http://www.google.com
posted by autojack at 1:47 PM on May 12, 2008
RewriteCond %{REMOTE_ADDR} ^71\.5\.1\.212$
RewriteRule .* http://www.google.com
posted by autojack at 1:47 PM on May 12, 2008
The above is known to work for me, so at worst it seems you have a problem with your REQUEST_URI line. If you don't need that part (if you want to redirect all requests), you can leave it out and it should work.
Otherwise, when in doubt, #apache on irc.openprojects.net is my go-to source for mod_rewrite help.
posted by autojack at 1:50 PM on May 12, 2008
Otherwise, when in doubt, #apache on irc.openprojects.net is my go-to source for mod_rewrite help.
posted by autojack at 1:50 PM on May 12, 2008
The rewrite rules look fine to me although I've been working on Apache 2 for a while so there might be some 1.3 silliness going on.
I'd guess that the problem is that the REMOTE_ADDR is not the value you're expecting. Perhaps there's a proxy server in the way? An easy way to test would be to temporarily change the RewriteRule to read:
RewriteRule ^/(.*) http://test.foo.com/$1?%{REMOTE_ADDR} [R]
and load the page up in your favourite browser to see what happens.
posted by quiet at 5:26 PM on May 12, 2008
I'd guess that the problem is that the REMOTE_ADDR is not the value you're expecting. Perhaps there's a proxy server in the way? An easy way to test would be to temporarily change the RewriteRule to read:
RewriteRule ^/(.*) http://test.foo.com/$1?%{REMOTE_ADDR} [R]
and load the page up in your favourite browser to see what happens.
posted by quiet at 5:26 PM on May 12, 2008
Response by poster: I'll check that out, but the REMOTE_ADDR is my own machine, and the server is on the same subnet as I am.
posted by Oktober at 7:04 AM on May 13, 2008
posted by Oktober at 7:04 AM on May 13, 2008
Not that I don't love mod_rewrite, but if REMOTE_ADDR is your own machine wouldn't it be easier to just use something like this:
http://pastie.caboo.se/196390
http://httpd.apache.org/docs/1.3/vhosts/name-based.html
http://httpd.apache.org/docs/2.0/mod/core.html#servername
posted by systematic at 1:55 PM on May 13, 2008
http://pastie.caboo.se/196390
http://httpd.apache.org/docs/1.3/vhosts/name-based.html
http://httpd.apache.org/docs/2.0/mod/core.html#servername
posted by systematic at 1:55 PM on May 13, 2008
Response by poster: it's my machine for testing purposes, but eventually, i'll have to have multiple staff members hitting it.
posted by Oktober at 10:46 AM on May 16, 2008
posted by Oktober at 10:46 AM on May 16, 2008
This thread is closed to new comments.
RewriteEngine On
RewriteLog /tmp/log.txt
RewriteLogLevel 9
This will log every step apache takes while churning your request through mod_rewrite. You get to see what apache is actually doing and you should be able to figure out where your logic is failing . Bonus OS X tip; use the Console app in the "/Application/Utilities" folder to monitor the log file. It's kind of a GUI for tail.
Don't forget to turn logging off once you go to production, as it'll make your server crawl.
posted by alana at 1:42 PM on May 12, 2008