我使用此重写代码重写对.php
文件的所有请求:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*)$ $1.php [L]
因此,当我输入index
时,index.php
将会加载,或者如果我输入contact
,contact.php
将会加载。
但是当我输入一个不存在的名称时,我会收到内部服务器错误,例如我输入了此名称test
或fdgdfg/ewrtt/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.
我的ErrorDocument 404 error.php
也有.htaccess
。
这是我完整的.htaccess
文件:
ErrorDocument 404 index.php
RewriteEngine on
RewriteBase /shop
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*)$ $1.php [L]
答案 0 :(得分:1)
您需要检查php文件是否确实存在,否则您拥有的2个条件将始终失败并且.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]
答案 1 :(得分:1)
以下解决了我的问题 -
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^\.]+)$ $1.php [NC]