mod_rewrite以前的规则影响其他规则

时间:2016-11-29 17:28:58

标签: php apache .htaccess mod-rewrite url-rewriting

我有下一个RewriteRule可以访问

domain.com/section/products/whatever

来自名为_products的文件夹。原始网址如下所示:

domain.com/section/_products/product.php?param=whatever

这是规则:

RewriteRule ^section/products/(.*)$ section/_products/$1 [L,NC]

现在我需要重写这个网址:

domain.com/section/products/brand/index.php?brand=apple

对此:

domain.com/section/products/brand/apple

我正在尝试这个:

Options -Indexes
Options +FollowSymlinks
Options -MultiViews
RewriteEngine on
RewriteBase /

RewriteRule ^section/products/(.*)$ section/_products/$1 [L,NC]

RewriteRule ^(section/products/brand)/([\w-]+)$ /section/_products/brand/index.php?brand=$1 [QSA,L,NC]

还有这个:

RewriteRule ^(section/products/brand)/([\w-]+)$ /section/products/brand/index.php?brand=$1 [QSA,L,NC]

这两个人都没有为我工作。

section/_products/$1规则应该在其他规则之后还是之前? 我无法找到正确的逻辑。

1 个答案:

答案 0 :(得分:1)

你可以这样:

Options +FollowSymlinks -Indexes -MultiViews
RewriteEngine on
RewriteBase /

# skip all files and directories from rewrite rules below
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]

RewriteRule ^(section/products/brand)/([^/]+)$ $1/index.php?brand=$2 [QSA,L,NC]

RewriteRule ^(section)/products/(.*)$ $1/_products/$1 [L,NC,QSA]