Apache Internal Redirect Infinite Loop with mod_rewrite

Okay, this seems impossible to solve, even with all the solutions already given to similar problems.

Have .htaccess file with the following code:

RewriteEngine on

RewriteBase /amit/public/

RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule .* - [L]

RewriteCond ${REQUEST_FILENAME} !-f
RewriteCond ${REQUEST_FILENAME} !-d
RewriteRule ^(.*[^/])/?$ $1.php [NC,L]

Without the RewriteCond %{ENV:REDIRECT_STATUS} 200 and the rule following the condition, the server goes through infinite loop. The apache log file would look like this:

r->uri = /amit/public/activities.php.php.php.php.php.php.php.php
redirected from r->uri = /amit/public/activities.php.php.php.php.php.php.php
redirected from r->uri = /amit/public/activities.php.php.php.php.php.php
redirected from r->uri = /amit/public/activities.php.php.php.php.php
redirected from r->uri = /amit/public/activities.php.php.php.php
redirected from r->uri = /amit/public/activities.php.php.php
redirected from r->uri = /amit/public/activities.php.php
redirected from r->uri = /amit/public/activities.php
redirected from r->uri = /amit/public/activities

Essentially, it will NOT open the file when it evaluates to requested_path.php but will keep on adding more .phps.

If I do add in that condition, it will not add .php at all and return 404 error with this line in my log:

File does not exist: /Applications/MAMP/htdocs/amit/public/activities

Busted closed to 24 hours trying to solve it, no luck...

1 answer

  • answered 2018-07-11 12:44 Anonymous

    You’re using $ instead of % for %{REQUEST_FILENAME}:

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*[^/])/?$ $1.php [NC,L]
    

    That would still cause a redirect loop when the file doesn’t exist. You can check if the extension was added too:

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*[^/])(?<!\.php)/?$ $1.php [NC,L]