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 = Fullstack Developer. Book Author. Teacher. Human Being.
Banhammer: Protect your WordPress site against threats.

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

  1. Jeff Starr
    Jeff Starr 2008/09/01 9:37 am

    @Hamster: The code presented in the article will work with any web-served document, XHTML, HTML, PHP, or otherwise. You will need access to an htaccess file (or the Apache config file) to implement it, but once in place, it should work just fine.

  2. Great bit of code, I came to comments as I had a question and found my answer here :) Thanks to the coder!

  3. Jeff Starr
    Jeff Starr 2008/09/17 8:32 am

    My pleasure, Emmx, thanks for the positive feedback :)

  4. Would this work with myspace?

  5. Jeff Starr
    Jeff Starr 2008/09/24 2:42 pm

    Hi lenny,
    No, unfortunately the redirect method described in the article is a server-side technique that requires server access to implement. I think MySpace recently restricted server access to staff only, sadly..

  6. I’ve setup the redirect in my .htaccess above – but has anyone else found that it slows down regular access to the web site?

    I tested blocking my IP – took a while for the redirected page to appear, when I tried it from another connection – I couldn’t access my site, it just stayed there trying to find the page.

    Is this something I’ve done?

  7. Jeff Starr
    Jeff Starr 2009/02/15 9:30 am

    @Sheps: Nope, Apache is great at handling simple redirects (and even complicated ones!). Granted, using mod_rewrite is not the optimal way of identifying IP addresses, but it is needed to accomplish the URL-specific redirect. Perhaps there is another issue involved..?

  8. How to redirect using .htaccess

    from:
    mysite.com/index.php?option=com_jdownloads&Itemid=49&task=viewcategory&catid=9

    to:
    mysite.com/index.php?option=com_jdownloads&Itemid=49&task=viewcategory&catid=7

    it ‘s not working if use %{REQUEST_URI}

  9. Jeff Starr
    Jeff Starr 2009/02/21 7:44 pm

    @Almir: Try targeting the query string via the %{QUERY_STRING} variable.

  10. can you post example using query string, please

  11. Jeff Starr
    Jeff Starr 2009/02/25 4:38 pm

    Sure, here is a basic example of how to target the query string:

    <ifmodule mod_rewrite.c>
       RewriteCond %{QUERY_STRING} target\-string\-01 [NC,OR]
       RewriteCond %{QUERY_STRING} target\-string\-02 [NC]
       RewriteRule .* - [F,L]
    </ifmodule>

    That should provide enough information to facilitate further research via Google or your favorite search engine. Replace each of the target strings to match your desired query string and you should be good to go!

  12. This works great for redirecting a specific requested page from a specific IP to a custom page on your site.

    Is it possible to redirect for any/all pages requested on the site. I still want to send that specific IP request to a custom page just for them.

    # 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]

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 »
USP Pro: Unlimited front-end forms for user-submitted posts and more.
Thoughts
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 :)
Newsletter
Get news, updates, deals & tips via email.
Email kept private. Easy unsubscribe anytime.