A RewriteRule loops when the target of the rule matches the initial rewrite pattern. For example, if you were to rewrite all URLs starting with "foo" to "foo.php", then "foo.php" would also get rewritten to "foo.php", and so on, forever.

For example, consider the following rule:

RewriteRule ^/example http://server.com/example.html [R]

A simple redirect. The resulting URL matches the RewriteRule (because it starts with /example and redirects. And again. And again. You get the idea.

This is solved by prefacing your RewriteRule with a RewriteCond to catch that condition, thus:

RewriteCond %{REQUEST_URI} !^/example\.html$
RewriteRule ^/example http://server.com/example.html [R]

That says the same thing as the previous recipe, except that it also says to only run the rule if the incoming request doesn't look like example.html. Problem solved. No more looping.

RewriteLooping (last edited 2009-09-20 22:13:33 by localhost)