1

我使用此重写代码将所有请求重写到.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]
4

2 回答 2

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]
于 2013-09-04T14:28:32.757 回答
1

以下解决了我的问题 -

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^\.]+)$ $1.php [NC]
于 2015-02-08T14:38:53.493 回答