.htaccess mod_rewrite规则冲突

时间:2012-03-15 00:23:21

标签: regex apache .htaccess mod-rewrite

我有以下重写规则:

    Options +FollowSymLinks
    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} !\.(css|gif|ico|jpg|js|png|swf|txt)$


    RewriteCond %{REQUEST_URI} !^/(Account|Logout|Password|Tags) [NC]
    RewriteRule ^([^/]*)$ /index.php?city=$1 [QSA,L]


    RewriteCond %{REQUEST_URI} ^/Account [NC]
    RewriteRule ^Account /members/account.php [NC,L]

    RewriteCond %{REQUEST_URI} ^/Logout [NC]
    RewriteRule ^Logout /members/logout.php [NC,L]

    RewriteCond %{REQUEST_URI} ^/Password [NC]
    RewriteRule ^Password /members/password.php [NC,L]

    RewriteCond %{REQUEST_URI} ^/Tags [NC]
    RewriteRule ^Tags /members/tags.php [NC,L]

我正在尝试添加另一个条件,以便将其加载为

  domain.com/$city/$provider/$name

这是规则:

  RewriteRule ^([^/]*)/([^/]*)/([^/]*)$ /listing.php?city=$1&provider=$2&urlname=$3 [L]

我的问题是此规则与我的索引文件的规则冲突,后者加载为domain.com/$city。

我非常感谢任何建议。

谢谢!

1 个答案:

答案 0 :(得分:1)

你的任何规则都没有错。它只是第一个涵盖了它将接受的最后一个。避免此问题的最简单方法是将更严格的规则移到更一般的规则之上。即在这种情况下,将上市规则放在index.php规则之前。

附录

糟糕, - > listing.php后跟 - > indexp.php规则仍会循环,因为rule1会在第一次传递时触发,然后在第二次传递时触发rule2,因为为了正则表达式匹配而剥离查询字符串在一个规则;和“listing.php匹配^([^/]*)$

在模式与现有文件不匹配的情况下,您应该只匹配^([^/]*)$。防止这种情况的方法是放一个

RewriteCond %{REQUEST_FILENAME} !-f
在index.php规则之前

相关问题