Fall Sale! Code FALL2024 takes 25% OFF our Pro Plugins & Books »
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 = Creative thinker. Passionate about free and open Web.
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 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 .* 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. 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 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 »
Digging Into WordPress: Take your WordPress skills to the next level.
Thoughts
I disabled AI in Google search results. It was making me lazy.
Went out walking today and soaked up some sunshine. It felt good.
I have an original box/packaging for 2010 iMac if anyone wants it free let me know.
Always ask AI to cite its sources. Also: “The Web” is not a valid answer.
All free plugins updated and ready for WP 6.6 dropping next week. Pro plugin updates in the works also complete :)
99% of video thumbnail/previews are pure cringe. Goofy faces = Clickbait.
RIP ICQ
Newsletter
Get news, updates, deals & tips via email.
Email kept private. Easy unsubscribe anytime.