View Full Version : IP redirection on Apache server

06-07-2004, 11:48 PM
Hi, I am trying to accomplish the following:

-- redirect various visitors to my website to other sites, based on their IP. As I want to exclude various classes of IP, how would this be done with PHP. So far I have the following:

$ipi = getenv("REMOTE_ADDR");
if(eregi("111(.*)", $ipi) || eregi("222(.*)", $ipi))
echo "<META http-equiv='Refresh' content='0; url=welcome.html'>";
echo "<META http-equiv='Refresh' content='0; url=index2.html'>";

In this case, welcome.html uses a redirect function to send people to somewhere.com.

In the above case, the IPs of the form 111.xxx.xxx.xxx and 222.xxx.xxx.xxx are restricted.

Anybody who can tell me how would I restrict 111.123.134.xxx and so on? This would restrict a very limited domain.

Can I use the same function?


06-08-2004, 11:19 PM
It's probably easier to use a .htaccess-file for that:

put this in it:

order allow,deny
deny from
deny from 123.34.5.
deny from 123.
deny from .codingforums.com
allow from all

needles to say that you should change the ip-blocks and domain.
you can block users per domain or IP. I just put some possible values in it.

maybe this could help too:
(original: http://originalsinner.net/pig/tutorials/htaccess.htm )
This is a script that will redirect IP's that you don't want viewing your site. (I set up an account on geocities for this script so that people would know they had been blocked and why.)

RewriteEngine on
RewriteCond %{REMOTE_ADDR} 00\.00\.00\.
RewriteRule .* http://www.youralternativedirectory/yourwarningpage.htm [R]

*Remember to change the IP address (00\.00\.00\.) to whatever IP you are blocking, but keep it in the same format! Also, change the URL to the directory (not a directory on your site) that you can link to to warn them that they have been blocked.


06-10-2004, 01:37 PM
Thanks a lot!!! I will look into it...However, I might not be able to do it like this since I get my hosting from an external server, and I am not sure if I have access to the Apache configuration specifically.

In the meantime, would there be a PHP solution for this?