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 = Designer. Developer. Producer. Writer. Editor. Etc.
Digging Into WordPress: Take your WordPress skills to the next level.

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

  1. Jeff Starr
    Jeff Starr 2009/08/31 3:03 pm

    Hi Baruch, it should be do-able using Apache’s QUERY_STRING parameter. Use something like:

    <IfModule mod_rewrite.c>
     RewriteCond %{QUERY_STRING} hop [NC]
     RewriteRule (.*) http://www.mywebsite.com/index.html [R=301,L]

    Haven’t tested this, but the logic is sound and should get you started.

  2. Hi Jeff,

    Really appreciate your help so far. I have forgotten to mention that all affiliate links containing the parameter “hop” go to index.html by default.

    So, for instance http://www.mywebsite.com/?hop=affiliatename is directed to index.html. What I have no idea about is how to redirect the rest of the hop-less traffic to index2.html

  3. Jeff Starr
    Jeff Starr 2009/08/31 8:27 pm

    Just as we are targeting query strings containing “hop”, we can target query strings that don’t contain it by using a “not” operator (exclamation point) before the target string:

    <IfModule mod_rewrite.c>
     RewriteCond %{QUERY_STRING} !hop [NC]
     RewriteRule (.*) http://www.mywebsite.com/index2.html [R=301,L]

    This would catch everything without hop in the query string and redirect it to the alternate index page.

  4. Hi Jeff,

    The last code is working, or not :\ . Firefox gets to index2.html, but the page displays the following error 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.<<

    IE gets to index2.html after 15 minutes of loading. The page is displayed without formatting, pictures, etc. but at least it’s there.

    I don’t know what to think about it. If you have any ideas please let me know and thank you from the bottom of my heart for your help!


  5. Jeff Starr
    Jeff Starr 2009/09/01 9:05 pm

    Ah, you might want to try adding this just above the rewrite rule:

    RewriteCond %{REQUEST_URI} !/index2.html$

    That should do the trick..

  6. Jeff, you are a genius!:))
    The script is working flawlessly now! I’m so relieved!

    Thank you sooo much for taking the time to tweak the code for me and patiently answering my questions.

    Big THANKS!:)


  7. Jeff Starr
    Jeff Starr 2009/09/02 8:32 am

    @Baruch: you’re welcome :)

  8. Jeff, I have been researching the net for tips on redirecting traffic to specific pages depending on their IP address. So far you material is the soundest and most complete so I know you are going to be able to help me.

    Here is the question:

    I need to redirect traffic from my Index.html to a specific page depending on the country of origin of the IP address.

    I have a block of 400+ IP ranges for a specific country and have tried the following code with no results. I have included only three IP ranges to simplify the example. The last IP is mine so I can check if the code works.

    RewriteEngine on
    RewriteBase /
    RewriteCond %{REMOTE_HOST}
    RewriteCond %{REMOTE_HOST} 333.444.0.0/9
    RewriteCond %{REMOTE_HOST} 555.666.0.0/9
    RewriteCond %{REMOTE_HOST} 777.888.99.10
    RewriteCond %{REQUEST_URI} ^/$
    RewriteRule .* /default.html [R=301,L]

  9. Jeff Starr
    Jeff Starr 2009/09/14 2:43 pm

    Hi Cristian, the first thing that jumps out at me when looking at this code is the fact that it is impossible to satisfy the given conditions. As written, your rewrite conditions are saying effectively:

    If the remote host is from this IP range and this IP range and this IP range, and the requested URI is root, then apply the redirect rule.”

    What you want to say, rather, is this:

    If the remote host is from this IP range or this IP range or this IP range, and the requested URI is root, then apply the redirect rule.”

    The key here is to add [OR] flags at the end of the appropriate conditions. Something like this:

    RewriteCond %{REMOTE_HOST} [OR]
    RewriteCond %{REMOTE_HOST} 333.444.0.0/9 [OR]
    RewriteCond %{REMOTE_HOST} 555.666.0.0/9 [OR]
    RewriteCond %{REMOTE_HOST} 777.888.99.10
    RewriteCond %{REQUEST_URI} ^/$
    RewriteRule .* /default.html [R=301,L]

    Lines without the explicit [OR] flag are interpreted in additive fashion, which is why your code fails — it is impossible for any single host to be coming from multiple IPs.

  10. Jeff ,

    Thanks it works ! at least redirects when I go to the main page.

    Now I have to contact someone within the particular IP block to see if does there too.


  11. Jeff, I am trying to redirect to a certain page according to the IP address for a geographic region.

    With your help (Sep 14) so far I have the following:

    RewriteEngine on
    RewriteBase /
    RewriteCond %{REMOTE_HOST} [OR]
    RewriteCond %{REMOTE_HOST}
    RewriteCond %{REQUEST_URI} ^/$
    RewriteRule .* /default.htm [R=301,L]

    This works perfectly when I include my specific IP to test it (or anyone else’s ) but it seems to be ignoring the IP ranges using CIDR notation.

    I am trying to redirect to specific pages depending on the country or origin of tbe request to my index page.

    Thanks again,


  12. Jeff Starr
    Jeff Starr 2009/10/19 7:56 am

    Hi Cristian, not sure what might be happening here, but there is another notation that you can use whereby the last octet of the IP address is omitted from the rewrite condition.

    By omitting one (or more) of the octets, you essentially create a wildcard address that will match anything beginning with whatever octets remain.

    I hope it helps!

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 »
Banhammer: Protect your WordPress site against threats.
What's up with Plesk UI lately? Especially on Chrome it looks just awful, all kinds of broken. Come on Plesk devs get it together.
Things get stressful, I try to pray. Not always easy, but always helps to relax and regain focus.
Nice new speed checker at fastorslow.com.
Easy way to exclude certain tests from WP Site Health: Site Health Tool Manager
Excellent (and free) tool for getting tons of site SSL infos: whynopadlock.com
Everyone just stay home and hide forever. Brilliant idea.
Playing with Microsoft Edge browser on macOS. It's like 1998 all over again.