Search code examples
.htaccessmod-rewriteexpressionengine

Adding a slash causes error


Here is my rewrite code:

    RewriteEngine On
    RewriteBase "/"
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d

    RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
    RewriteRule (.*)$ http://www.whatever.com/$1/ [R=301,L]

    RewriteRule ^(.*)$ index.php/$1 [L]

This is for an expression engine site. If I take these 2 lines out, the site works fine:

    RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
    RewriteRule (.*)$ http://www.whatever.com/$1/ [R=301,L]

Those are the lines I added to add a trailing slash to any URL if it doesn't have one, to avoid duplicate indexing in Google. It results in this error in my log file: Request exceeded the l "Limit of 10 internal redirects due to probable configuration error"

I assume its not processing the RewriteCond right and entering an infinite loop. Any thoughts as to why this is happening?


Solution

  • First, the principle: after any rewrite, the entire ruleset is processed from the beginning. The [L] flag does not prevent this; it controls whether the rule is the last in the current run.

    In this case the problem is the last rule. It rewrites anything like this:

    foo → index.php/foo → index.php/index.php/foo → ...
    

    When you remove the two lines, this rule gets the !-f and !-d conditions attached, which prevents the loop.

    From the grouping of the lines it looks like you want to attach those two conditions to all following rules. You can't do that, but instead you can attach the opposite conditions to a rule that will stop processing:

    RewriteCond %{REQUEST_FILENAME} -f [OR]
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule ^ - [L]