.htaccess两个重写规则之间的冲突


.htaccess conflict between two rewrite rule

在我的网站两个重写规则是相互冲突的我有url为我的用户如= http://twekr.com/sam我所有的网页都打开了http://twekr.com/login

现在这里是htaccess文件规则

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^'.]+)$ $1.php [NC,L]
RewriteRule ^([a-zA-Z0-9_-]+)$ profile.php?user=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ profile.php?user=$1

现在只有这个规则起作用,因为它是先加载的=

RewriteCond %{REQUEST_FILENAME} !-d

如果我把profile重写规则放在上面

RewriteRule ^([a-zA-Z0-9_-]+)$ profile.php?user=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ profile.php?user=$1 (then this works)

RewriteCond %{REQUEST_FILENAME} !-d (does not work)

htaccess规则有什么问题?

您应该在添加.php扩展名之前检查.php文件的存在:

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(['w-]+)/?$ profile.php?user=$1 [L,QSA]