Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I understand as far as what is happening with my code as to why I'm getting a infinite loop. So I have the following:

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteRule ^foo/?([^/]+)?/?(.+)? /foo.php?bar=$1&baz=$2 [L,NC]
</IfModule>

So, what is happening is the user is coming in at foo (or foo/a/b) and getting redirected to foo.php?bar=$1&baz=$2. But at this point (and this is where I know I need a more thorough explanation, since I'm sure this is by design) the .htaccess file is being rerun on foo.php?bar=$1&baz=$2, which matches my rule, and goes into an infinite loop. But I don't know why it is being rerun through my rules.

What is it that I'm not understanding about .htaccess RewriteRules?

PS I have solved this immediate problem placing the following commands before the rule, but I would like to know why so I can fix the problem in the future (and so I can remove these lines and do other things instead).

RewriteCondition %{REQUEST_FILENAME} !-f
RewriteCondition %{REQUEST_FILENAME} !-d
share|improve this question

2 Answers 2

up vote 1 down vote accepted

First request gets processed and the URL is rewritten. This results a new request for the new URL. Without some way to filter the requests (see Devin's answer) what you're seeing is correct behaviour.

share|improve this answer
    
Is there anyway to, after the first request, tell it to not process it again without using RewriteCond? –  Ktash Feb 11 '12 at 3:58
    
@Ktash, I don't think so but I don't understand why you don't just use RewriteCond. It's far easier to use standard commands than to try to hack around them. –  John Gardeniers Feb 11 '12 at 4:58
    
Yeah, I wasn't necessarily thinking of 'hacking around them'. I just didn't know if there was a standard way to do it. My main hesitation for using RewriteCond is that I'd wanted to redirect users who tried to hit the php pages directly. Guess it's not possible though. Thanks. –  Ktash Feb 11 '12 at 5:05

It is common to need a RewriteCond before a RewriteRule, so as not to match the rewritten URL and avoid an infinite loop. That's one of the most common uses for RewriteCond. You should feel good about figuring that out, and not feel like you're doing something wrong by using it that way.

share|improve this answer

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.