Super Plugin Sale! Your Choice: BOGO or 30% Off »
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!

About the Author
Jeff Starr = Fullstack Developer. Book Author. Teacher. Human Being.
USP Pro: Unlimited front-end forms for user-submitted posts and more.

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

  1. Jeff Starr 2010/05/04 9:24 pm

    Shouldn’t be a problem.. just keep in mind that the htaccess files/directives execute in cascading fashion. I.e., it is possible for your subdirectory rules to override those in your root file.

  2. I have a stalker that I am trying to redirect and I am using your code in htaccess but it isn’t working.

  3. Thekla, post the code you are using, with each line wrapped in <code> tags, and someone will take a look at it..

  4. Thank you. Your help is greatly appreciated.

    RewriteEngine On
    RewriteBase /
    RewriteCond %{REMOTE_HOST} xx\.xxx\.xxx\.xxx
    RewriteCond %{REQUEST_URI} /terridelights\.com$
    RewriteRule .* /stalkeralert.html[R=301,L]

  5. Jeff Starr 2010/05/07 1:07 pm

    Instead of REMOTE_HOST, try with REMOTE_ADDR, and then also remove the second RewriteCond:

    RewriteEngine On
    RewriteCond %{REMOTE_ADDR} ^789.123.456.3$
    RewriteRule .* [R=301,L]

    Notice the full URL in the RewriteRule. Here is more information on mod_rewriting using eight different variables.

  6. I tried what you put verbatim and I get this message

    The page isn’t redirecting properly
    Firefox has detected that the server is redirecting the request for this address in a way that will never complete.
    * This problem can sometimes be caused by disabling or refusing to accept

    I checked in my internet options and am currently accepting cookies.

    Please help

  7. Jeff Starr 2010/05/07 1:21 pm

    Ah yes, let’s try this:

    RewriteEngine On
    RewriteCond %{REMOTE_ADDR} ^789\.123\.456\.3$
    RewriteCond %{REQUEST_URI} !/stalkeralert.html$ [NC]
    RewriteRule .* [R=301,L]

    I forgot about the infinite loop ;)

  8. Am I your biggest pest?

    I redid it with what you posted, changed the ip of course, and got the same message as before.

  9. Jeff Starr 2010/05/07 1:51 pm

    Hmm, not sure what’s going on.. maybe try placing an [OR] at the end of the second first RewriteCond..?

  10. I have been trying to get this for days. I tried the OR as you have it and, I hate to say that it isn’t working. I called my hosting company and they said it should accept the .htaccess file. If you can think of something else, please let me know. In the meantime, I will keep searching.

  11. Thanks! This was exactly what I was looking for!

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 »
BBQ Pro: The fastest firewall to protect your WordPress.
Crazy that we’re almost halfway thru 2024.
I live right next door to the absolute loudest car in town. And the owner loves to drive it.
8G Firewall now out of beta testing, ready for use on production sites.
It's all about that ad revenue baby.
Note to self: encrypting 500 GB of data on my iMac takes around 8 hours.
Getting back into things after a bit of a break. Currently 7° F outside. Chillz.
Get news, updates, deals & tips via email.
Email kept private. Easy unsubscribe anytime.