500 Error when '/' in url with url rewriting

59 Views Asked by At

I have this .htacess on my server to redirect example/test -> example/test.php.

ErrorDocument 404 http://example.net/404

RewriteEngine on

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [L]

RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.*)$ $1.html [L]

It works for basic url but when I request "example/fakefolder/test" it return a 500 error.

And i have nothing in the logs saying i have a 500 error.

1

There are 1 best solutions below

0
On BEST ANSWER

The problem you're seeing is that the -f check when paired with %{REQUEST_FILENAME} is sort of a "fuzzy" check. It includes pathinfo style matching, which means if you have a file /script.php and the request is /script/blahblah, the condition %{REQUEST_FILENAME}.php -f actually returns true because it fuzzy matches: /script.php/blahblah, which exists as a file, via pathinfo style URL.

You need to be explicit about your path and include the possible /.

ErrorDocument 404 http://rafflehaven.net/404

RewriteEngine on

RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.*?)/?$ $1.php [L]

RewriteCond %{DOCUMENT_ROOT}/$1.html -f
RewriteRule ^(.*?)/?$ $1.html [L]