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 RewriteRule
s?
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
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.
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.