April 11, 2021 0 Comments

Last Updated on April 13, 2021 by amitoverflow1

One of the most common issue with using RewriteRule
in an htaccess file is the infinite loop error.

This problem doesn’t occur if you are using a server.cofig file
instead of .htaccess because server.config file is read only ones while the htaccess is read by server each time something is requested from the server.
Too many redirects error htaccess

If you are getting a too many redirect error on your server then there is probably something wrong with your rule’s configuration .
In this article I will show you a solution to solve this error but before we jump to the solution , let’s first understand why this problem occurred and what’s a loop in mod-rewrite.

Redirect loop vs RewriteLoop

There are two types of loops in RewriteRule . RewriteLoop is when a file or directory is
internally pointing to itself. This can be due to a catch-all unconditional rule with (.*) pattern. Apache webserver returns a 500 error status when there is an internal loop.

Redirect loop is when a file or directory redirects back to itself . This can be due to an unconditional rule with a catch-all pattern. Web browser like chrome will show you a “Too many redirect” error .

The following rule will generate a 500 error because it doesn’t have any RewriteCond(s) and it uses a catch-all pattern :

RewriteEngine on

RewriteRule .* /index.php [L]

If you visit any URL on your site you will get an infinite error with the rule above. This is because the rule has a catch-all pattern which also matches the destination path /index.php .

The following code will generate a Redirect loop error as it uses a catch all pattern and no conditions are applied to the rule :

RewriteEngine on

RewriteRule .* /index.php [L,R]

With the rule above if you visit any URL of your site , the rule will redirect you to /index.php and then the request will go in an endless loop .

htaccess file is read by server more then one time to serve a request . For example , with the rule above if you type /myfile.php the rule will first redirect myfile.php to /index.php and then to process the destination file /index.php
your server again reads the htaccess, index.php matches the rule’s pattern and gets redirected to itself. This redirection results in a infinite loop error.

How to prevent the loop error in RewriteRule

The most common cause of the loop error in an htaccess file is a RewriteRule with a catch-all regex pattern like (.*) . If your htaccess has RewriteRule with this pattern then put some conditions above the rule to protect it

For example the following two conditions can help solve your RewriteRule looping problem

  • RewriteCond %{REQUEST_FILENAME} !-f
  • RewriteCond %{REQUEST_FILENAME} !-d

These two conditions prevents your existent files and directories from being rewritten.

Now let’s use these two conditions with our RewriteRule :

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) /index.php [L]

You won’t get any error with the rule above , because it’s protected by conditions. These two conditions prevents your existent files and
directories from being rewritten. Since the /index.php in the rule above is an existent file , this won’t match the pattern while internal rewriting process.

How to quick fix Rewrite loop error

If you have an htaccess file with hundreds of RewriteRules and other directives then it can sometimes be hard to figure out which rule is causing the problem, in that case you can
just put the following anti-loop RewriteRule at the top of your htaccess to fix the problem:

RewriteEngine on
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^ - [L]

This terminates all the internal rewriting process as soon as the request completes.

Leave a Reply

Your email address will not be published. Required fields are marked *