Most of us who want to keep a user out of their site use the Cpanel IP block tool. I am a dork and prefer to learn stalkers something, so I love to fiddle myself with my htaccess file. The code to redirect a specific IP to an interesting site about cyberstalking is simple.
Add the following code to your htaccess file (of course don’t forget to use the IP you want to redirect).
Redirect Single IP To Any Site With htaccess
RewriteCond %{REMOTE_ADDR} ^64\.126\.81\.30$
RewriteRule .* http://www.usdoj.gov/criminal/cybercrime/cyberstalking.htm [R,L]
If you want to redirect more IP, just add them in a new line, right under the first IP, but you’ll have to add [OR] at the end of every line with an IP. Except at the last line!.
Redirect Several IPs To Any Site With htaccess
RewriteCond %{REMOTE_ADDR} ^64\.126\.81\.30$ [OR]
RewriteCond %{REMOTE_ADDR} ^75\.117\.244\.27$
RewriteRule .* http://www.usdoj.gov/criminal/cybercrime/cyberstalking.htm [R,L]
It’s as simple as that, and much more fun to be informative than just ban someone IMNSHO.
Note that [R,L] belongs on the same line as the URL you want to use.
12 have made me smarter ↓
1 Jinge // Jun 4, 2007 at 8:12 pm// View all comments by Jinge//
+0
I can’t make it work..
Let’s say I want to redirect only 200.200.200.200 to my http://bilderblogg.se
RewriteCond %{REMOTE_ADDR} ^200\.200\.200\.200$
RewriteRule .* http://bilderblogg.se/index.php[R,L]
Only that to lines in my .htaccess and nothing happens when I test it.
Yes, I change 200.200 etc to my own ip-number…
What did I do wrong?
Rgds Jinge
2 Franky // Jun 4, 2007 at 8:40 pm// View all comments by Franky//
+0
Try with a [space] (empty) in between the URL and [R,L]. And also a space after RewriteCond and RewriteRule
3 Franky // Jun 4, 2007 at 8:43 pm// View all comments by Franky//
+0
But be aware, when you do that to your own IP, I recommend you to proxy to your site, because the results might be funny.
4 Tom // Jul 17, 2007 at 8:35 am// View all comments by Tom//
+0
Is it possible to redirect ANYONE who doesn’t belong to a set of IPs? kind of the opposite of what you’ve done here. I want to redirect everyone who doesn’t belong to a particular subnet.
Cheers
5 jonathon // Jan 21, 2008 at 2:45 pm// View all comments by jonathon//
+0
thanks, just what i been looking for to redirect nightmare spammers, away from my blog and forums.
6 Dwayne // Oct 5, 2008 at 12:49 am// View all comments by Dwayne//
+0
Thank You, Thank You, Thank You!
After much searching, and way too much trial and error, this is the only one I found to actually work.
Lets see how he likes the gay porn site I sent him to…LOL
7 Slim0123 // Mar 6, 2009 at 2:04 pm// View all comments by Slim0123//
+0
What if we need to redirect a range of ips and not just a few ips…..
8 gdox // Mar 27, 2009 at 7:06 pm// View all comments by gdox//
+0
Slim0123, it uses regular expressions to filter the ip’s so you just need to modify that part to match the range you require.
^ = matches from the start of the string
$ = matches to the end of the string
so if I remember correctly to get all ip’s start with ^\d+\.\d+\.\d+\.\d+$ and make it more specific as you go.
the following should catch all ip’s begining with 200:
^200\.\d+\.\d+\.\d+$
this next line would catch all ip’s starting with 200.200
^200\.200\.\d+\.\d+$
and so on…
9 gdox // Mar 27, 2009 at 7:27 pm// View all comments by gdox//
+0
to get a true range say from 200.200.200.1 to 200.200.200.50 you would need something more complex such as
$200\.200\.200\.([1-9]|1[0-9]|2[0-9]|3[0-9]|4[0-9]|50)$
[1-9] = matches any digit between 1 & 9
| = OR
so it’s saying match 200.200.200……1 to 9 OR 10 to 19 OR 20 to 29 OR 30 to 39 OR 40 to 49 OR 50
10 Adhound // May 12, 2009 at 12:52 am// View all comments by Adhound//
+0
I’d like to automatically redirect bad bots & scrapers etc back to their own IP. Is that possible using htaccess? i.e. if any visitor meets a rewrite condition, then grab their IP and redirect back to it.
11 Liam Sand // Dec 30, 2009 at 11:58 pm// View all comments by Liam Sand//
+0
Good article.
Anyone wanting to effect a whole subnet range, an easy way would be like this.
RewriteCond %{REMOTE_ADDR} ^200\.200\.200
RewriteRule .* http://www.usdoj.gov/criminal/cybercrime/cyberstalking.htm [R,L]
Also works leaving the “\.” at the end of that line.
This and other combos would work also.
RewriteCond %{REMOTE_ADDR} ^200\.200
12 Rob Wise // Jan 8, 2010 at 10:06 pm// View all comments by Rob Wise//
+0
On a slightly different tact we have used a 301 redirect inside the htaccess file to point to our new fixed IP address as we were transitioning DNS. wanting to avoid having potentially 2 live data tables and having to reconcile those once the DNS transition had finished.
Everyone ended up on the new box interacting with the one database while the transition was occurring.