Save 25% on our pro plugins with coupon code: SPRING2023
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 = Creative thinker. Passionate about free and open Web.
.htaccess made easy: Improve site performance and security.

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

  1. Avatar photo
    Jeff Starr 2009/07/19 9:16 pm

    Hi Tony, you should be able to remove that last RewriteCond and get the desired results. You may also want to check out this technique, which requires a password for any specified IPs, allowing open access to everyone else.

  2. Jeff:

    Thanks for the response. I have been struggling with this for a few days and can’t get it to work. I had already tried it without the last RewriteCond and it still fails. I think what’s happening is, because the IP address is blocked, when the last line ties to send the IP address to the/just-for-you.html page, it gets caught in a loop that is blocking the /just-for-you.html page .

    I tried adding this to the htaccess file and that did not work.

    order allow,deny
    allow from all

    I also included the same code to allow for any image or sound files that were part of the /just-for-you.html page. – And it still loops.

    Now I was testing this with my IP address, so maybe that has something to do with it. But when I set up a ErrorDocument 403 custom 403 page, I tested it with my IP address and it worked.

    I will try the password technique (thanks) but this is driving me crazy that I can’t get it to work.

  3. Avatar photo
    Jeff Starr 2009/07/20 9:25 pm

    Tony, perhaps, but keep in mind that we are not blocking anything here, only redirecting requests to a specific page.

    By the way, how are you testing the target IP address? Are you using two different IP addresses to test with? The first thing to do is make sure the rewrite works by removing the IP condition and just seeing if everyone is redirected. Then, if that works, try blocking a target IP via 403 without the page redirect.

    This may not yield fruit, but is where I would begin testing if I had the time.

    Let me know how the password-protection method goes. You may even find what you need from that code to rig up something that is more in line with your stated objectives.

    Good luck :)

  4. Jeff:

    You’re right. I’m not blocking anything, just redirecting. I don’t know why I had it in my head that this was a block.

    But now I’m really stumped. I thought there might be something in my htacess file that was causing a conflict with this rewrite. So I removed everything except this to get a clean test:

    RewriteEngine On
    RewriteBase /
    RewriteRule .* /just-for-you.html [R=301,L]

    As suggested I took out the IP condition and it still choked. What’s weird is that if I change the last line to a page somewhere else on the web, i.e., not a page on my site, it works fine. Or if I add:

    RewriteCond %{REQUEST_URI} /requested-page.html$

    which if someone is already at the site and wants access to a specific requested page, it redirects just fine. But if I try to redirect from entering the site at all, it chokes.

    I even tried a different browser. With IE, the page just doesn’t load. Just sits there blank page – no information. When I tried to access the page with Google’s Chrome, It still would not access the page, but it did indicate this error message:

    the web page at ( has resulted in too many redirects. Clearing your cookies for this site may fix the problem. If not, it is possibly a server configuration issue and not a problem with your computer. Error 310 (net::ERR_TOO_MANY_REDIRECTS): There were too many redirects.

    Tried clearing the cookies, rebooting, cleared the temp Internet file and still nothing. Maybe there is something goofy on the server side with Apache that is out of whack.

    Blocking via 403 works with the system 403 page or custom page but that is the page everybody gets. Can you have different 403 pages. i.e. one for everyone and others tied to a specific IP?

    Haven’t tried the password method yet because I wanted to see if I could solve this first. Perhaps I will try that and move on if I can’t get this resolved.

    Sorry for the long post.

  5. Hi Jeff,

    Thanks for the details and instructions on how to redirect. This article is a great resource.

    I have one question, I am using your above mentioned code:

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

    The problem I am having is that I am trying to redirect my home page, so I don’t know what to enter as the requested-page. I tried my index page but the redirect didn’t work. Any idea how to redirect the root page of the website?

    thanks for your time.

  6. Avatar photo
    Jeff Starr 2009/07/26 8:18 am

    Hi Peter, have you tried just using a forward slash:


    Haven’t tested, but it should work.

  7. Quick question on using the htaccess files to perform redirection…. Basically the need I have is to redirect anyone that hits a certain directory except for the localhost.

    So if anyone manages to hit the images drectory I use for my site, they will be redirected to a gallery page. But if I simply do the

    Redirect 301 /images/directory/

    This kills any image I have used in a webpage because it is not filtering out localhost from the redirect.

    Is there any way to do this??

  8. Allrighty…. So afer having a fiddle with this I stumbled upon a remarkable thing that answered my previous question…. If you perform the 301 redirect using mod_rewrite, it no longer breaks any images that are embedded into pages.

    So this entry in htaccess

    Redirect 301 /images/subdir/ /images/index.php

    breaks any embedded images as the localhost is not excluded from the redirect.

    But this entry in htaccess

    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_URI} /images/dir/$
    RewriteRule .* /images/index.php [R=301,L]

    does not break any of it…

    I have a suspicion that it may also have the side affect of killing hotlinking to the images from other sites… Have not tested that thought at all.

    Thanks Jeff for writing the original article and everyone else for commenting, which gave me plenty to think on and experiment with.

    I guess that voodoo wins everytime eh

  9. Avatar photo
    Jeff Starr 2009/08/19 4:09 pm

    Hi Simon, glad to hear you got it sorted and working. HTAccess is nothing but voodoo, bordering on black magic at times. I have spent many a sleepless night wrestling with such directives, only to discover hours later a missing dot. Always a learning experience for sure.

  10. Jeff… hehe I know what you mean about missing a character in a string. Many a drinking session has been caused by that scenario….

    I need to straigten out a furfie I said in my last post…

    I mistakenly referred to local host being being included or excluded from the redirect.
    This is not the case. The rule I posted above only works on the directory name, hence excluding the actual file from the rewrite.

    I also checked as to whether it also kills the direct linking and it does not. Looking back I am not sure why I thought it would.

    Sorry to ramble on, I don’t wish to be the cause of misinformation though with that previous comment. If I managed to get anything wrong again this time round, feel free to correct me.

  11. Avatar photo
    Jeff Starr 2009/08/23 8:09 am

    Thanks for following up with the clarification, Simon. You may have just saved the next guy from hitting the bottle. Always good to be thorough with plenty of documentation. Cheers.

  12. Hi Jeff,

    I’m a novice Clickbank publisher with just a handful of affiliates, whom I feel I need to cherish and take care of:). Similarly I’m a complete noob when it comes to server side scripts and such. I’ve been looking for a solution to my problem for few good weeks now and I would really appreciate you help.

    I’m looking for a way to separate traffic coming to from affiliate links, from the rest of the traffic, and send them to different pages. As Clickbank passes the affiliate’s nickname to the vendor in a parameter called “hop”, I would like all page requests containing parameter “hop” (*/?hop=) to be directed to where customers can pay using Clickbank gateway, making my affiliates happy, whereas redirect all the remaining traffic to where a different payment processor is set up.
    Is this something that can be done to .htaccess? If so, how?
    I would be eternally grateful for any help.



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 »
The Tao of WordPress: Master the art of WordPress.
Excellent (and free) tool to test your site's SSL configuration.
Plugin updates! All of our free and pro plugins ready for WordPress 6.2.
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 :)
Get news, updates, deals & tips via email.
Email kept private. Easy unsubscribe anytime.