2

我正在使用 apache rewrite_mod (Apache/2.2.17 Win32) 并遇到 rewriteRule 的非常奇怪的行为。

我的脚本主要重写f1 .. f<infinity>从 nice url 命名的无限过滤器参数,并在循环中将它们添加为查询变量、添加路径作为查询和页码。

它完美无缺,但如果我添加另一个规则(脚本中的最后一条规则)

RewriteRule   ^(.+)\.html$                       /index.php?path=$1.html [QSA]

对于另一种情况,它会在脚本开始时更改重写器的执行。




输入网址:

http://testing.loc/some/thing/index0-f1-nice-cars-f2-planes-f3-karts-f4-bike.html

所有重写后的 PHP 预期结果:

$_SERVER[QUERY_STRING] => path=some/thing/&page=0&f1=nice-cars&f2=planes&f3=karts&f4=bike
$_SERVER[SCRIPT_NAME] => /index.php

脚本如下所示(下一部分功能完美):

# from: some/thing/index0-f1-nice-cars-f2-planes-f3-karts-f4-bike.html
# to:   some/thing/index0.html?f=-f1-nice-cars-f2-planes-f3-karts-f4-bike

RewriteRule   ^(.*/?index[0-9]*)((?:-f[0-9]+-.+?)+)\.html$   /$1.html?f=$2 [QSA]

# from: some/thing/index0.html?f=-f1-nice-cars-f2-planes-f3-karts-f4-bike
# to    index.php?path=some/thing/&page=0&f=-f1-nice-cars-f2-planes-f3-karts-f4-bike

#              (   $1   )     (  $2  )
RewriteRule   ^(.+/{0,1})index([0-9]*)\.html$    /index.php?path=$1&page=$2 [QSA]

# while in f is something like "f1-nice-cars" (for example)
# remove "f1-nice-cars" from f and add as query "f1=nice-cars"

#                              (%1)    (   %2  ) ( %3)(       %4       )
RewriteCond   %{QUERY_STRING} ^(.*)&f=-(f[0-9]+)-(.+?)((?:-f[0-9]+-.+)*)$ 
RewriteRule   ^index\.php$                       /index.php?%1&%2=%3&f=%4 [L]

# remove empty "f=" from query

RewriteCond   %{QUERY_STRING} ^(.*)&f=$ 
RewriteRule   ^index\.php$                       /index.php?%1

在这里,规则之后是 url 的形状:

index.php?path=some/thing/&page=0&f1=nice-cars&f2=planes&f3=karts&f4=bike

如果脚本中没有下一条规则,一切都会完美运行。但是如果我添加它,这条规则本身什么也不做,但是 rewrite_mod 在脚本开始时添加了更多的东西。

# rewrite rule for other paths without filters

RewriteRule   ^(.+)\.html$                       /index.php?path=$1.html [QSA]

实际上在运行所有脚本后结果是:

index.php?path=/some/thing/index0.html/some/thing/index0&f1=nice-cars&f2=planes&f3=karts&f4=bike
4

1 回答 1

0

I found resolution of this problem. When 2 rules match URL at one pass of .htaccess, then apache rewrite_mod use method "add path info postfix" (its something like bug or feature), which ruin path of request. So when I added flag [L] to that 2 rules, result is OK.

I thinked that apache pass .htaccess sequentially and does not matter on next rules...

于 2012-05-29T11:49:55.497 回答