Am I Famous Now…

...Creator And Destroyer

HOWTO : htaccess IP Redirect To Any Site

April 9th, 2007 by WebMatrix · 12 Made Me Smarter

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.

Tags: , , , , // 16,192 Views

12 have made me smarter ↓

  1. Gravatar

    1 Jinge // Jun 4, 2007 at 8:12 pm// View all comments by Jinge//  Add karma Subtract karma  +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. Gravatar

    2 Franky // Jun 4, 2007 at 8:40 pm// View all comments by Franky//  Add karma Subtract karma  +0

    Try with a [space] (empty) in between the URL and [R,L]. And also a space after RewriteCond and RewriteRule

  3. Gravatar

    3 Franky // Jun 4, 2007 at 8:43 pm// View all comments by Franky//  Add karma Subtract karma  +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. Gravatar

    4 Tom // Jul 17, 2007 at 8:35 am// View all comments by Tom//  Add karma Subtract karma  +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. Gravatar

    5 jonathon // Jan 21, 2008 at 2:45 pm// View all comments by jonathon//  Add karma Subtract karma  +0

    thanks, just what i been looking for to redirect nightmare spammers, away from my blog and forums.

  6. Gravatar

    6 Dwayne // Oct 5, 2008 at 12:49 am// View all comments by Dwayne//  Add karma Subtract karma  +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. Gravatar

    7 Slim0123 // Mar 6, 2009 at 2:04 pm// View all comments by Slim0123//  Add karma Subtract karma  +0

    What if we need to redirect a range of ips and not just a few ips…..

  8. Gravatar

    8 gdox // Mar 27, 2009 at 7:06 pm// View all comments by gdox//  Add karma Subtract karma  +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. Gravatar

    9 gdox // Mar 27, 2009 at 7:27 pm// View all comments by gdox//  Add karma Subtract karma  +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. Gravatar

    10 Adhound // May 12, 2009 at 12:52 am// View all comments by Adhound//  Add karma Subtract karma  +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. Gravatar

    11 Liam Sand // Dec 30, 2009 at 11:58 pm// View all comments by Liam Sand//  Add karma Subtract karma  +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. Gravatar

    12 Rob Wise // Jan 8, 2010 at 10:06 pm// View all comments by Rob Wise//  Add karma Subtract karma  +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.

Comment Policy

Spam comments are not supported and will be deleted.

This site supports a dofollow policy for comments, accordingly I do reserve the right to remove your URL if your comment seems to be nothing more than a plug to your site. This is a non-discutable decision from my side.

Stay polite and ad rem in the comments. We all, me too, have had our child days, but have grown up. Flame comments will be deleted.
If you really want to flame, feel free and do it on your own blog. Feel free to link back.

Off topic comments will be deleted.