服务器在使用htacess时抛出LimitInternalRecursion错误

我对.htaccess.htaccess而我只是没有深入了解发生了什么事情。 服务器只是说,即使我看不到任何理由“redirect”,超过了10个内部redirect的限制。 它只发生在我尝试使用/users/Username/users/显示一个错误,该用户无法find这是我想要的, /users/Username引发500内部服务器错误。 文件本身是好的,数据库检查也应该工作。 .htaccess文件是:

 RewriteEngine On RewriteCond %{HTTPS} !=on RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301] RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC] RewriteRule ^(.*)$ https://%1/$1 [R=301,L] RewriteCond %{REQUEST_FILENAME}.php -f RewriteCond %{REQUEST_URI} !/$ RewriteRule (.*) $1\.php [L] IndexIgnore * ErrorDocument 400 / ErrorDocument 404 / RewriteRule ^users/([a-zA-Z0-9_-]+)/?$ users.php?id=$1 [NC,L] 

 RewriteCond %{REQUEST_FILENAME}.php -f RewriteCond %{REQUEST_URI} !/$ RewriteRule (.*) $1\.php [L] : RewriteRule ^users/([a-zA-Z0-9_-]+)/?$ users.php?id=$1 [NC,L] 

第一个规则块是捕获/users/Username的请求。 在这种情况下, REQUEST_FILENAME.../users (有效目录之后的第一个完整path段),而.../users.php确实是一个有效的文件。 但是,您将.php附加到所请求的URL的末尾,因此/users/Username将变为/users/Username.php等。

你可以通过简单地颠倒这些指令的顺序来解决这个冲突:

 RewriteRule ^users/([a-zA-Z0-9_-]+)/?$ users.php?id=$1 [L] RewriteCond %{REQUEST_FILENAME}.php -f RewriteCond %{REQUEST_URI} !/$ RewriteRule (.*) $1.php [L] 

现在, /users/Username的请求将被第一个规则捕获,并正确地路由到user.php?id=Username ,这将无法匹配第二个规则块。

(我删除了NC标记,因为你已经在模式中检查a-zA-Z ,除非/users也可以是/uSeRs ?!)