Lemon Drop
Lemon Drop

Reputation: 2133

500 Error from non-existant files/folders

Whenever I try to access a file/folder that doesn't exist using my .htaccess, it returns a 500 error instead of a 404 and the following message in the logs:

Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace.

Here is the code causing the issues:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/([^/]+)/*$ /$1.php?p=$2 [L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/*$ /$1.php [L]
</IfModule>

The code converts urls as follows:

http://mysite.com/about -> http://mysite.com/about.php
http://mysite.com/something/else -> http://mysite.com/something.php?p=else

A url causing errors is really any that the server cant find e.g.:

http://mysite.com/Idontexist.php
http://mysite.com/nosuchfolder

Does anyone know how to prevent this?

Upvotes: 1

Views: 78

Answers (1)

Amine Hajyoussef
Amine Hajyoussef

Reputation: 4440

you have to add a RewriteCond which check if Mod_Rewrite has already made the internal redirection, the L falg stop the current run only, then .htaccess directives will be re-executed again if the url has been changed (which is the case here):

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !\.php$
RewriteRule ^([^/]+)/([^/]+)/*$ /$1.php?p=$2 [L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !\.php$
RewriteRule ^([^/]+)/*$ /$1.php [L]
</IfModule>

Upvotes: 2

Related Questions