Welcome to the new design! Please report any bugs or issues, thanks :)
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 = Web Developer. Book Author. Secretly Important.
.htaccess made easy: Improve site performance and security.

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 .* http://terridelights.com/stalkeralert.html [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 .* http://terridelights.com/stalkeralert.html [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.
Playing the long game.
They have weaponized the idiots.
Good software never steals focus from the user. Even during startup.
After 10 years running my own business, I still manage schedules and tasks using old school post-it notes, sometimes simple sometimes very elaborate.
You know those sites, where you're trying to just grab a quick bit of information but the page is shifting all over the place as it loads up 3 million advertisements.
Selling two of my top WordPress domains, wp-zen.com & zen-wp.com $300 for both. Aged 9 years. Drop a line if interested.
Never force your users to type out a password (or any long string of characters) by blocking the paste function. Typing long strings leads to MORE errors than simple copy/paste.