Take the 2-minute tour ×
Webmasters Stack Exchange is a question and answer site for pro webmasters. It's 100% free, no registration required.

I'm trying to send a particular IP, that is trying to get one page on my site, to another page, and do this through my .htaccess file. I've seen many general descriptions of how to do it (in many syntactically different ways) but frankly none of them work. I put these command s, of the sort

RewriteEngine On
RewriteCond %{REMOTE_ADDR} ^99\.888\.777\.66$
RewriteRule ^/askedforpage\.html /redirecttopage.html [R,NE,NC] 

in my .htaccess file ... but these commands simply leave my site non-functional. I'm using Apache 2.2, on a Mac with OS 10.6.8.

How do I do this??


Here is my .htaccess file, without the "deny" commands for malicious IPs.

Header set Accept-Ranges none   
order allow,deny     
allow from all   
Redirect permanent xyz.html http://xxyyzz.com/header.htm
share|improve this question
    
"leave my site non-functional"? Those commands shouldn't actually do anything in a per-directory .htaccess file, since ^/askedforpage\.html (with a slash prefix) will never match - do you have anything else in your htaccess file? Also, "I'm trying to send a particular IP" - presumably you don't mean "send"? You are wanting to redirect based on the IP address? –  w3d Feb 20 at 16:43
    
Yes, I have a number of IP-denials there (Chinese, mostly), and they work fine. Also a Range restriction. As to leaving my site non-functional, I notice that's what errors in an .htaccess do in general. Yes "send". When an IP asked for "askedforpage.html", I want to SEND them to "redirecttopage.html". I only want to do that for one IP, so yes, this is based on the IP address. –  user618 Feb 20 at 17:07

2 Answers 2

up vote 0 down vote accepted

There's not really very much wrong with the code you have posted. There are certainly no errors in your code that should leave your site non-functional. As it stands it is syntactically valid. However, check your line endings to make sure you have no spurious characters - this can cause problems.

Try the following:

Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REMOTE_ADDR} ^99\.888\.777\.66$
RewriteRule ^askedforpage\.html /redirecttopage.html [R,NC,L]

I have removed the / (slash) prefix in the RewriteRule pattern. The directory prefix is removed in per-directory .htaccess files before the pattern is matched (so URL patterns never start with a slash). I have also removed the NE flag - this is unnecessary in the code you have posted (although is not an error).

EDIT: I've added the L (LAST) flag, although to be honest it shouldn't make much of a difference in this case, providing there are no conflicting rules in your (or another) .htaccess file - which there don't appear to be from your question.

It is admittedly more usual to specify the L flag in RewriteRule external redirects since generally you do not want the rewrite engine to reprocess your rules after you have established the redirect.

share|improve this answer
    
Well, I coped precisely what you suggested in your code block into my .htaccess file, and I checked all line endings to make sure there were no spurious characters. As a result, I can't get anything off my server. I get a "Forbidden You don't have permissions to access ... on this server" message for everything I try to access. –  user618 Feb 20 at 18:35
    
... I got cut off ... This was after I made some dummy "askedforpage.html" and "redirecttopage.html" files. I get that error for several differerent browsers. As soon as I remove those lines from my .htaccess file, everything is fine again. I think I need some diagnostics. I'm stumped. –  user618 Feb 20 at 18:47
    
Is mod_rewrite enabled? (But even if it isn't, those directives should simply be ignored.) At the moment I can only assume it is conflicting with other directives in your htaccess file, since that code by itself should not cause any problems. The only possible error, that I can see, would be a 404 if "redirecttopage.html" did not exist. Could you paste your .htaccess in its entirety into your question so we can take a look? –  w3d Feb 20 at 19:08
    
OK, here is my .htaccess file (minus the "deny" commands to dispense with malicious IPs). My apologies, but I don't know how to create a list. I put in two spaces after a line, but that doesn't add a line break. The formatting in this comments section is kinda weird. I've put a # sign before each command. #Header set Accept-Ranges none #order allow,deny #allow from all #Redirect permanent xyz.html xxyyzz.com/header.htm –  user618 Feb 20 at 20:33
1  
Success! The first thing I tried was adding the "L", as you did. It worked! Everything else stayed the same. Now, my server doesn't give me errors when I ask for stuff, and when the designated IP (and only that IP) tries to get askedforpage.html, they get redirecttopage.html instead. Many thanks, not only for the critical information, but for your patience in dealing with me! –  user618 Feb 21 at 2:40

This is just to report that the advice of w3d was exactly right. The way you do this is

RewriteEngine On  
RewriteCond %{REMOTE_ADDR} ^99\.888\.777\.66$    
RewriteRule ^askedforpage\.html /redirecttopage.html [R,NC,L]

(I already had FollowSymLinks on, and mod_rewrite enabled.) The "L" (LAST) flag is important!

These .htaccess commands look for a request by IP 99.888.777.66 (and only that IP) for the page "askedforpage.html", and return the page "redirecttopage.html" instead. Unless otherwise denied, that IP can get anything else.

Tip 'o the bowler to w3d.

share|improve this answer
    
You're welcome, glad you got it working OK. However, on the Stack Exchange network it is preferable to mark the (other) answer as "accepted" - if this is what helped you - by clicking on the tick to the left of the answer, rather than answering your own question with what is essentially a comment (which is likely to get deleted by moderators). By accepting an answer, it takes the question out of the unanswered question queue, informing other users. (However, you need 15+ rep points before you can upvote an answer, so answer or ask another question! Welcome to Webmasters. :) –  w3d Feb 21 at 10:43

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.