skip to Main Content

I have .htaccess file with the following directives:

RewriteEngine on

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

Pretty basic redirection. However, I’m getting an infinite internal loop in my apache log files with a 500 error in the browser. The apache log file has the following:

r->uri = /amit/public/activities.php.php.php.php.php.php.php.php.php
redirected from 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/activities.php
redirected from r->uri = /activities.php

That’s when I enter the page with the extension. Makes no difference when I try to access the page without the extension. The page exists and I’ve double checked everything, but it seems to me that the main issue is the RewriteCond.

Any help is appreciated.

2

Answers


  1. Chosen as BEST ANSWER

    Thanks to @anubhava. I've just realized that my code uses $ for all the server variables. It should be %. A silly typo had hacked my brain for close to 24 hours... So the 'ultimate' code with the suggestions made by @anubhava would be:

    Options +FollowSymlinks
    RewriteEngine on
    
    RewriteBase /amit/public/
    
    RewriteCond %{ENV:REDIRECT_STATUS} ^$
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-s
    RewriteCond %{REQUEST_FILENAME} !-l
    RewriteRule ^(.*[^/])/?$ $1.php [NC,L]
    

    Works like a charm.


  2. You don’t need 4 conditions and need to have them ANDed together:

    RewriteEngine on
    RewriteBase /amit/public/
    
    RewriteCond %{ENV:REDIRECT_STATUS} ^$
    RewriteCond %{REQUEST_FILENAME} !-l
    RewriteCond ${REQUEST_FILENAME} !-d
    RewriteRule ^(.+?)/?$ $1.php [L]
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search