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!
83 responses to “Permanently Redirect a Specific IP Request for a Single Page via htaccess”
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]
</IfModule>
Haven’t tested this, but the logic is sound and should get you started.
Hi Jeff,
Really appreciate your help so far. I have forgotten to mention that all affiliate links containing the parameter “
hop
” go toindex.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 toindex2.html
…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]
</IfModule>
This would catch everything without
hop
in the query string and redirect it to the alternate index page.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!
Baruch
Ah, you might want to try adding this just above the rewrite rule:
RewriteCond %{REQUEST_URI} !/index2.html$
That should do the trick..
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!:)
baruch
@Baruch: you’re welcome :)
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} 111.222.0.0/9
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]
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} 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 %{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.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.
Thanks
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} 11.222.0.0/17 [OR]
RewriteCond %{REMOTE_HOST} 44.55.64.0/19
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,
Cristian
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!