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 = Web Developer. Security Specialist. WordPress Buff.
Banhammer: Protect your WordPress site against threats.

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

  1. Avatar photo
    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. Avatar photo

    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. Avatar photo
    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
    cookies.

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

    Please help

  7. Avatar photo
    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. Avatar photo
    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.
Welcome
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 »
WP Themes In Depth: Build and sell awesome WordPress themes.
Thoughts
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.
Newsletter
Get news, updates, deals & tips via email.
Email kept private. Easy unsubscribe anytime.