Apache 文件或文件夹不存在时出现内部服务器错误

Apache 文件或文件夹不存在时出现内部服务器错误,apache,.htaccess,mod-rewrite,internal-server-error,Apache,.htaccess,Mod Rewrite,Internal Server Error,我使用此重写代码将所有请求重写到.php文件: RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule (.*)$ $1.php [L] 因此,当我输入index时,将加载index.php,或者如果我输入contact时,将加载contact.php 但当我输入一个不存在的名称时,我将得到内部服务器错误,例如,我输入了这个名称test或fdgdfg/ewrt/ddfghdfg,在此

我使用此重写代码将所有请求重写到
.php
文件:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*)$ $1.php [L]
因此,当我输入
index
时,将加载
index.php
,或者如果我输入
contact
时,将加载
contact.php

但当我输入一个不存在的名称时,我将得到内部服务器错误,例如,我输入了这个名称
test
fdgdfg/ewrt/ddfghdfg
,在此之后我将得到以下错误:

Internal Server Error

The server encountered an internal error or misconfiguration and was unable to complete your request.

Please contact the server administrator, webmaster@mydomain.com and inform them of the time the error occurred, and anything you might have done that may have caused the error.

More information about this error may be available in the server error log.

Additionally, a 500 Internal Server Error error was encountered while trying to use an ErrorDocument to handle the request.
我的
.htaccess
中也有
ErrorDocument 404 error.php

这是我完整的
.htaccess
文件:

ErrorDocument 404 index.php
RewriteEngine on
RewriteBase  /shop
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*)$ $1.php [L]

您需要检查php文件是否确实存在,否则您遇到的两个条件将始终失败,并且
.php
扩展名会不断追加,例如,您最终会得到一个URI,如下所示:

/shop/foo.php.php.php.php.php.php.php.php.php.php.php.php.php.php.php
直到达到内部递归限制,并得到500个错误。尝试添加其他条件以检查文件是否存在:

ErrorDocument 404 index.php
RewriteEngine on
RewriteBase  /shop
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule (.*)$ $1.php [L]

下面解决了我的问题-

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^\.]+)$ $1.php [NC]

您确定您收到的是内部服务器错误而不是无限重定向循环消息吗?请尝试使用其他浏览器以确保安全。