New Bookstore! Save 20% on books with discount code: LAUNCH
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.
GA Pro: Add Google Analytics to WordPress like a pro.

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

  1. Jeff Starr
    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. Jeff Starr

    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
    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
    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
    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 »
The Tao of WordPress: Master the art of WordPress.
LOL @ “weekly” support: “Your request has been received and will be reviewed soon. Neil (your support guy) checks support requests weekly. Rest assured we’ll get to it!” — Gotta be kidding with this.
After several days of work, finally taking a break. If anyone needs me, I'll be in Zora’s Domain, working thru Mah Eliya shrine.
Never ever call the client a useless idiot. Even if it’s true.
Inevitable that Google shuts down Feedburner. Maybe this year? Who knows. Here is a nice summary plus alternatives.
There is no way that Marvel can serve up anything better than what they've already done with Infinity saga/Endgame. Knowhere to go but down, sadly.
I built a new search launchpad to make my life easier. So I can search multiple engines quickly and easily.
Updating all muh plugins for WP 5.5 imminent release! Wut August :)
Get news, updates, deals & tips via email.
Email kept private. Easy unsubscribe anytime.