我对.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
?!)