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
Jeff Starr – July 19, 2009
Hi Tony, you should be able to remove that last
RewriteCondand get the desired results. You may also want to check out this technique, which requires a password for any specified IPs, allowing open access to everyone else.Tony – July 20, 2009
Jeff:
Thanks for the response. I have been struggling with this for a few days and can’t get it to work. I had already tried it without the last RewriteCond and it still fails. I think what’s happening is, because the IP address is blocked, when the last line ties to send the IP address to the/just-for-you.html page, it gets caught in a loop that is blocking the /just-for-you.html page .
I tried adding this to the htaccess file and that did not work.
order allow,deny
allow from all
I also included the same code to allow for any image or sound files that were part of the /just-for-you.html page. – And it still loops.
Now I was testing this with my IP address, so maybe that has something to do with it. But when I set up a ErrorDocument 403 custom 403 page, I tested it with my IP address and it worked.
I will try the password technique (thanks) but this is driving me crazy that I can’t get it to work.
Jeff Starr – July 20, 2009
Tony, perhaps, but keep in mind that we are not blocking anything here, only redirecting requests to a specific page.
By the way, how are you testing the target IP address? Are you using two different IP addresses to test with? The first thing to do is make sure the rewrite works by removing the IP condition and just seeing if everyone is redirected. Then, if that works, try blocking a target IP via 403 without the page redirect.
This may not yield fruit, but is where I would begin testing if I had the time.
Let me know how the password-protection method goes. You may even find what you need from that code to rig up something that is more in line with your stated objectives.
Good luck :)
Tony – July 21, 2009
Jeff:
You’re right. I’m not blocking anything, just redirecting. I don’t know why I had it in my head that this was a block.
But now I’m really stumped. I thought there might be something in my htacess file that was causing a conflict with this rewrite. So I removed everything except this to get a clean test:
RewriteEngine OnRewriteBase /RewriteRule .* /just-for-you.html [R=301,L]As suggested I took out the IP condition and it still choked. What’s weird is that if I change the last line to a page somewhere else on the web, i.e., not a page on my site, it works fine. Or if I add:
RewriteCond %{REQUEST_URI} /requested-page.html$which if someone is already at the site and wants access to a specific requested page, it redirects just fine. But if I try to redirect from entering the site at all, it chokes.
I even tried a different browser. With IE, the page just doesn’t load. Just sits there blank page – no information. When I tried to access the page with Google’s Chrome, It still would not access the page, but it did indicate this error message:
Tried clearing the cookies, rebooting, cleared the temp Internet file and still nothing. Maybe there is something goofy on the server side with Apache that is out of whack.
Blocking via 403 works with the system 403 page or custom page but that is the page everybody gets. Can you have different 403 pages. i.e. one for everyone and others tied to a specific IP?
Haven’t tried the password method yet because I wanted to see if I could solve this first. Perhaps I will try that and move on if I can’t get this resolved.
Sorry for the long post.
Peter – July 24, 2009
Hi Jeff,
Thanks for the details and instructions on how to redirect. This article is a great resource.
I have one question, I am using your above mentioned code:
RewriteEngine OnRewriteBase /RewriteCond %{REMOTE_HOST} 123\.456\.789RewriteCond %{REQUEST_URI} /requested-page\.html$RewriteRule .* /just-for-you.html [R=301,L]The problem I am having is that I am trying to redirect my home page, so I don’t know what to enter as the requested-page. I tried my index page but the redirect didn’t work. Any idea how to redirect the root page of the website?
thanks for your time.
Jeff Starr – July 26, 2009
Hi Peter, have you tried just using a forward slash:
^/$Haven’t tested, but it should work.
Simon – August 12, 2009
Quick question on using the htaccess files to perform redirection…. Basically the need I have is to redirect anyone that hits a certain directory except for the localhost.
So if anyone manages to hit the images drectory I use for my site, they will be redirected to a gallery page. But if I simply do the
Redirect 301 /images/directory/ http://site.com/gallery/index.phpThis kills any image I have used in a webpage because it is not filtering out localhost from the redirect.
Is there any way to do this??
Simon – August 16, 2009
Allrighty…. So afer having a fiddle with this I stumbled upon a remarkable thing that answered my previous question…. If you perform the 301 redirect using mod_rewrite, it no longer breaks any images that are embedded into pages.
So this entry in htaccess
Redirect 301 /images/subdir/ /images/index.phpbreaks any embedded images as the localhost is not excluded from the redirect.
But this entry in htaccess
RewriteEngine OnRewriteBase /RewriteCond %{REQUEST_URI} /images/dir/$RewriteRule .* /images/index.php [R=301,L]does not break any of it…
I have a suspicion that it may also have the side affect of killing hotlinking to the images from other sites… Have not tested that thought at all.
Thanks Jeff for writing the original article and everyone else for commenting, which gave me plenty to think on and experiment with.
I guess that voodoo wins everytime eh
Jeff Starr – August 19, 2009
Hi Simon, glad to hear you got it sorted and working. HTAccess is nothing but voodoo, bordering on black magic at times. I have spent many a sleepless night wrestling with such directives, only to discover hours later a missing dot. Always a learning experience for sure.
Simon – August 19, 2009
Jeff… hehe I know what you mean about missing a character in a string. Many a drinking session has been caused by that scenario….
I need to straigten out a furfie I said in my last post…
I mistakenly referred to local host being being included or excluded from the redirect.
This is not the case. The rule I posted above only works on the directory name, hence excluding the actual file from the rewrite.
I also checked as to whether it also kills the direct linking and it does not. Looking back I am not sure why I thought it would.
Sorry to ramble on, I don’t wish to be the cause of misinformation though with that previous comment. If I managed to get anything wrong again this time round, feel free to correct me.
Jeff Starr – August 23, 2009
Thanks for following up with the clarification, Simon. You may have just saved the next guy from hitting the bottle. Always good to be thorough with plenty of documentation. Cheers.
Baruch – August 28, 2009
Hi Jeff,
I’m a novice Clickbank publisher with just a handful of affiliates, whom I feel I need to cherish and take care of:). Similarly I’m a complete noob when it comes to server side scripts and such. I’ve been looking for a solution to my problem for few good weeks now and I would really appreciate you help.
I’m looking for a way to separate traffic coming to
www.mywebsite.comfrom affiliate links, from the rest of the traffic, and send them to different pages. As Clickbank passes the affiliate’s nickname to the vendor in a parameter called “hop”, I would like all page requests containing parameter “hop” (*/?hop=) to be directed towww.mywebsite.com/index.htmlwhere customers can pay using Clickbank gateway, making my affiliates happy, whereas redirect all the remaining traffic towww.mywebsite.com/index2.htmlwhere a different payment processor is set up.Is this something that can be done to .htaccess? If so, how?
I would be eternally grateful for any help.
Thanks,
baruch