如何解决.htaccess文件中的规则冲突


How do I resolve the conflict of rules in this .htaccess file?

如果我注释掉另一个,文件中的每一个规则都能很好地工作,但它们两者结合在一起并不能达到任何目的,反而会使网站变得丑陋。关于如何解决这个问题,有什么建议吗?谢谢

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
# If the request is for a valid directory
RewriteCond %{REQUEST_FILENAME} !-d
# If the request is for a valid file
RewriteCond %{REQUEST_FILENAME} !-f
# If the request is for a valid link
RewriteCond %{REQUEST_FILENAME} !-l
# do not do anything
RewriteRule ^ - [L]
# forward /blog/foo to blog.php/foo
RewriteRule ^blog/(.+)$ blog.php/$1 [L,NC]
# forward /john to user_page/profile.php?name=john
RewriteRule ^((?!blog/).+)$ user_page/profile.php?uid=$1 [L,QSA,NC]

尝试用RewriteRule ^(.+)$替换RewriteRule ^((?!blog/).+)$-无需再次检查blog字符串

我终于明白了。我修改了第二个重写条件,因为第二个改写规则重定向到一个不必要有效的url,即www.example.com/username到看起来像www.example.com/user_page/profile.php?user=username的预期有效url。

我还按照萨法罗夫的建议修改了最后/秒重写规则,它成功了!以下是完整的.htaccess文件。

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
# If the request is for a valid directory
RewriteCond %{REQUEST_FILENAME} -d [OR]
# OR If the request is for a valid file
RewriteCond %{REQUEST_FILENAME} -f  [OR]
# OR If the request is for a valid link
RewriteCond %{REQUEST_FILENAME} -l
# do not do anything
RewriteRule ^ - [L]
# forward /blog/foo to blog.php/foo
RewriteRule ^blog/(.+)$ blog.php/$1 [L,NC]
# forward /john to user_page/profile.php?name=john
RewriteRule ^(.+)$ user_page/profile.php?uid=$1 [L,QSA]