Save 25% on Wizard’s SQL for WP w/ code: WIZARDSQL
Web Dev + WordPress + Security

Permanently Redirect a Specific IP Request for a Single Page via htaccess

Not the most interesting title, but “oh well”..

Recently, a reader named Alison left a comment requesting help with a particular htaccess trick. She wanted to know how to permanently redirect (301) all requests for a specific page when requested from a specific IP address. In other words, when a visitor coming from 123.456.789 requests the page requested-page.html, the visitor will be redirected to just-for-you.html. All visitors not coming from that specific IP address are not redirected, and thus will see the originally requested page. Further, the redirect must apply only to requested-page.html, such that every visitor — including the one coming from 123.456.789 — will be able to see all of the other pages. Here is the htaccess code to make it happen:

# permanently redirect specific IP request for single page
RewriteEngine On
RewriteBase /
RewriteCond %{REMOTE_HOST} 123\.456\.789
RewriteCond %{REQUEST_URI} /requested-page\.html$
RewriteRule .* /just-for-you.html [R=301,L]

To use this redirect, simply edit the IP address, requested page, and redirect page. Copy and paste the code into your htaccess file and upload to your server. Test the 301 redirect via proxy and that’s it. Relax and enjoy!

Jeff Starr
About the Author
Jeff Starr = Designer. Developer. Producer. Writer. Editor. Etc.
Blackhole Pro: Trap bad bots in a virtual black hole.

83 responses to “Permanently Redirect a Specific IP Request for a Single Page via htaccess”

  1. Avatar photo
    Perishable 2008/04/22 7:30 am

    Hi Tweak, try the following code in your root htaccess file:

    <Files *>
      Order Allow,Deny
      Allow from all
      Deny from

    This should do the trick, however it would be better to block via range of IP addresses. Using the hostname to prevent access requires your server to execute a reverse-DNS request with the DNS network, thereby reducing performance for every server request.

  2. Jean Morgan 2008/06/12 5:57 am

    I need to bar a specific ISP from allowing any of their customers access to my website. Will this code in .htaccess do that?

  3. Avatar photo
    Perishable 2008/06/14 8:54 am

    Yup, should work fine! :)
    Here is another way of blocking a specific ISP via its IP address(es).

    <Files *>
     Order Allow,Deny
     Allow from all
     Deny from 123.123.123.
     Deny from 456.456.456.

    ..etc. You could also block via the ISP’s hostname, but there is a negative impact on performance because of all the reverse-DNS requests.

  4. I am trying to block an employer’s ISP from visiting my blog. It is outside their sphere of influence in my life however, I get constant criticism. I would like to redirect or block the ISP – not sure exactly if that is what this code is doing. I do not however want a message to come up that says, “You have been denied access to this page” – I just want it to be a frustrate them by redirects until they stop trying.
    I am novice enough that I don’t know for sure that I know where to put this code or which part of it I need – and need to edit.
    I apologize for being high maintenance about this – but any help you could offer would be greatly appreciated. Please do not comment ON my blog about this issue for the above stated reasons!

  5. Avatar photo

    Hi jewls, the code in the article should work fine for blocking a specific ISP. You will need to obtain the IP address of the ISP and then edit the code accordingly. The code should then be placed into your site’s web-accessible root HTAccess file. Then to display a custom error message, create a web page as desired and edit the HTAccess code to reflect the its location on your server. Once everything is in place, all visitors coming from the specified ISP will receive the “access-denied” message.

  6. Hi, I have a small fan-site that keeps being abused by one person who targets me from multiple family/friend computers. (136 contact form emails in 3 months is insane). I can see it’s him via online IP locators and they are always within a 50mile radius. There’s a real hotspot for activity in his area and it’s really annoying – I don’t get it from anywhere else. So far I’ve been collecting his specific IPs to block via a php script, but inevitably, the following week he uses a different computer and he’s at it again. The IPs are always different but generally the first 2 octets in common (A spate of attacks where the first 2 octets are always the same and on the next spate they’ve changed, but again, the first 2 octets are the same throughout that attack session – This is where I can see he is in a different town.)
    What I would like to do is block his ranges of IPs and refer him to a “You are not welcome” page. I’m not IP savvy so would it be best to block him based on the first 2 octets or be more specific and do it on 3? If it means blocking a handful of innocent visitors in his area, so be it – I just cant stand the continued abuse anymore.
    Also, what would the code be to do it?
    I’m sorry for the longwinded message but I’m at my wits end!

  7. Avatar photo

    Hi Bob, I understand your frustration with malicious harassment. Fortunately, the article above provides everything you need to handle this situation effectively and securely. As you say, however, there will be a considerable number of users who will no longer enjoy access to your site, but sometimes stopping malicious behavior is worth the sacrifice. Good luck!

  8. Here’s how to do an “entire” ISP. For some values of “do” … :-) I’ll use “” as the ISP “customer” I want to “do” …

    a) find the IP of the customer:
    user@host:~$ dig +short

    b) Find the containing netblock:
    user@host:~$ whois
    [examine output, looking for “netrange”, “cidr”, “inetnum”, or anything else that means “more than one IP”]
    user@host:~$ whois | grep -i cidr

    c) Create a regular expression from this:

    c1) Make sure a perl module is installed: CPAN – NetAddr::IP (404 link removed 2014/06/08) [debian unstable or ubuntu gutsy or hardy: sudo apt-get install libnetaddr-ip-perl; redhat: have fun with that …]

    c2) create an executable script with this in:
    user@host:~$ cat bin/netblock2regex
    use NetAddr::IP;
    my $ip = new NetAddr::IP $ARGV[0], $ARGV[1];
    print $ip->re(), "\n";

    c3) Use the script
    user@host:~$ ./bin/netblock2regex 18

    That string takes the place of the “123.\123\.123” in your htaccess file. Everything else stays the same.


    PS The string will probably get munged by the commenting system. There should be *no* animated smileys in your htaccess file … :-)

  9. Avatar photo
    Perishable 2008/07/04 5:55 pm

    Thanks for the info Jonathan. A few more explanatory notes and that comment would make an excellent article in and of itself. ;)

    I think your method raises an important point. I seemed to have missed the fact that ISPs generally employ an entire range of IP addresses, not just one, as I mistakenly suggested in previous comments. Even so, I think it would be much easier to block an entire ISP (based on its CIDR number) by simply using something similar to this in your site’s web-accessible root HTAccess file:

    # block ISP by CIDR
    <Files *>
       Order Allow,Deny
       Allow from all
       Deny from

    I am not that familiar with the perl language, so correct me if I am mistaken, but wouldn’t this technique be easier than the method described in your comment?


  10. Avatar photo

    Hi Jeff,
    Great article. I have one question though: How do I redirect a page that is being served up by WordPress’ rewrite rule?

    For example, I would like to have this page redirect if requested from a specific DNS. I’ve tried the following to no avail :/

    RewriteEngine On
    RewriteBase /
    RewriteCond %{REMOTE_HOST}
    RewriteCond %{REQUEST_URI} ./wordpress/page-to-block.php$
    RewriteRule ./wordpress/page-to-redirect.php [R=301,L]

    Thanks in advance :D

  11. Nice tips, hopefully it will be handy for me.

    I have been using toolator’s ip blocker for sometime now, which is basically pasting a html code onto the site. However they are now charging for there services, and i donät make that much money……lol.

    would all that, which has been written above, be used in html format.

    forgive me for being thick, but this is not my specialised subject.

  12. Avatar photo
    Jeff Starr 2008/09/01 9:35 am

    @Jeremy: I have yet to try this myself, but looking at your code, I might suggest removing the PHP file extension (i.e., \.php) from the second rewrite condition. Also, in the rewrite rule itself, you could try adding a string to match against, for example:

    RewriteRule (.*) http://domain.tld/target-file.php [R=301,L]

Comments are closed for this post. Something to add? Let me know.
Perishable Press is operated by Jeff Starr, a professional web developer and book author with two decades of experience. Here you will find posts about web development, WordPress, security, and more »
Digging Into WordPress: Take your WordPress skills to the next level.
Daylight savings is a complete waste of time and needs to be eliminated.
Got a couple of snow days here in mid-March. Fortunately it's not sticking.
I handle all email in real time as it comes in, perpetually clear inbox for years now.
Added some nice features to Wutsearch search engine launchpad. Now 21 engines!
.wp TLD plz :)
Nice collection of free SEO APIs and user-agent lookups for Googlebot, Bingbot, Applebot, YandexBot, and more.
90% of online customer support is just explaining how to do basic troubleshooting.
Get news, updates, deals & tips via email.
Email kept private. Easy unsubscribe anytime.