Latest TweetsNew version of Disable Gutenberg includes options to disable for specific theme templates and/or post/page IDs. wordpress.org/plugins/disable-…
Perishable Press

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.
Archives
83 responses
  1. I am trying to keep certain people from my website and am trying to use this script:

    Options +FollowSymLinks
    RewriteEngine On

    RewriteBase /

    RewriteCond %{REMOTE_HOST} 127.0.0.1
    RewriteRule .* /banned.htm [R=301,L]

    RewriteCond %{REQUEST_URI} "/functions/"
    RewriteRule (.*) $1 [L]
    RewriteRule ^([^.]+)/?$ /index.php?page=$1 [L]

    Now, it does and doesn’t work. First it seems to be in an eternal loop. Then I delete the lines from RewriteBase to [R=301,L] and all of a sudden my banned page comes up.

    What I want is to add lines to my .htaccess and redirect seveeralip’s to several different pages. This might seem silly, but for me it makes perfect sense. For some reason though, I keep failing unless I redirect to a different domain :(

    Can you help?

  2. Jeff Starr

    Hi Michel, isn’t 127.0.0.1 the IP address for localhost (i.e., your server)? That would explain why it only works when you redirect to an external domain.

  3. O crap…. yes. let me have the server listen on another address. This makes perfect sense of course. Thanks for pointing that one out.

  4. Thanks Jeff, just came across this post while trying to go live with a new site early so a client & myself could input content but everyone else would see a splash page.

    The [OR] variable for multiple IP addresses was getting me!

  5. Jeff Starr

    @Justin: Ah yes, I should have mentioned the [OR] flag for multiple IPs. For others who may need help:

    # permanently redirect specific IP request for single page
    RewriteEngine On
    RewriteBase /
    RewriteCond %{REMOTE_HOST} 123\.456\.789 [OR]
    RewriteCond %{REMOTE_HOST} 456\.789\.123 [OR]
    RewriteCond %{REMOTE_HOST} 789\.123\.456
    RewriteCond %{REQUEST_URI} /requested-page\.html$
    RewriteRule .* /just-for-you.html [R=301,L]

    Note the absence of the [OR] flag on the last IP condition.

  6. HuntingKnowledge April 6, 2010 @ 9:48 pm

    HI Jeff m

    I have a site up and running and a webautomated bot of an organisation is running is probing malacious requests changing the User agent so i blacklisted the access to the whole automised bot CIDR range by mentioning this in my .htaccess file

    order allow,deny
    deny from xx.xx.xx.x/xx
    allow from all

    When i check the access logs the bot still was able to get the confirmation of 302 confirmation from the server for /olddirectory/ in access logs and when i check the error logs the bot is getting rejected and access is denied some times may be from the root directory.

    As the bot is concentrated on knowing the existence/files in the /olddirectory/ i am thinking if this script works out

    # permanently redirect specific IP request for entire site

    Options +FollowSymlinks
    RewriteEngine on
    RewriteCond %{REMOTE_HOST} 22\.22\.22\.239
    RewriteRule /olddirectory/ http://www.newsite.com/ [R=301,L]

    let me know if i could deviate the bot and save my bandwith and divert the evil bot with the above included in my htaccess also let me know to include the whole range of CIDR and do the same..

    any help would be appreciated :)

  7. This page has been very helpful. I have a question. There is a person I want to put a redirect on when he accesses my site. But he is with a popular ISP where many of my friends also use. So I can’t simply block the ISP. The ISP changes his IP address frequently, so I can’t simply block a range of IPs without also blocking out my friends. I’ve monitored access log long enough to notice a pattern. Based on geography, his hostname always comes across as: fixedname.variablename.ispname.com, and with that I can pretty confidently say all accesses from fixedname.*.ispname.com is from this person. The ISP seems to divide the city into fixed blocks that I can tie the fixedname to this person’s location.

    Is there a way to say:
    RewriteCond %{REMOTE_HOST} fixedname.*.ispname.com?

    Thanks.

  8. Jeff Starr

    Hi Lisa, yes something like this should do it:

    RewriteCond %{REMOTE_HOST} fixedname\.(.*)\.ispname\.com [NC]
    RewriteRule .* - [F]

  9. Seems like a great tool. Together with the comments, I think I’ve found what I want to do, but there’s one thing I wonder if it works.

    I have this subsite after a /
    So basically www.mydomain.com/house is a different site than www.mydomain.com
    The site in the /house folder is both in Thai and in English, and I’m planning to use this script to redirect Thai IPs automatically to the Thai version, and all others to the English:

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

    Assuming I manage to find all (or at least most) Thai IP ranges, this should work, shouldn’t it? However, if I upload the htaccess file to the /house subdirectory, will it work? Or does such a file only work in the root directory?

  10. Jeff Starr

    Hi BenTrein, yes, I think so.. Logically, your code is saying this:

    • If the IP is address is within this range OR
    • if the IP address is within this range OR
    • if the IP address is within this range OR
    • if the requested URI is root..
    • Then redirect all requests to the thai subdirectory.

    If that sounds like what you are trying to do, then yes it should work as expected. If placed in the /house/ directory, the redirect will go to /house/thai/ directory. In other words, yes it should work in the subdirectory (instead of root), but the relative URI will be determined with the containing directory (i.e., /house/) as root.

  11. Thanks Jeff,

    I didn’t know you can have hostname as arguments to RewriteCond, I thought you could only have IP addresses. I’ll give that a try.

  12. Thanks a lot Jeff. I haven’t implemented it yet, but I’ll upload such a htaccess file to the sub-directory.

    If I have a different htaccess file in the root directory, will it create a conflict?

[ Comments are closed for this post ]