English 中文(简体)
当两个规则同时匹配 URL (添加路径信息后缀) 时, Apache 重新 write Rewrite Rules 意外行为
原标题:Apache RewriteRule unexpected behaviour when two rules match URL at one moment (add path info postfix)

I m playing with apache rewrite_mod (Apache/2.2.17 Win32) and encounter very weird behave of rewriteRule.

My script primarily rewrite infinite filter parameters named f1 .. f<infinity> from nice url and in loop adding them as query variables, add path as query, and page number.

It works flawlessly, but if I add one another rule (last rule in script)

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

对于其他情况,它会改变剧本开头的重写执行。




Input URL:

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

在这里,在规则是骨灰之后:

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

没有下一条脚本规则, 下一条规则是完全实用的 。 但是如果我加上它, 这条规则本身没有作用, 但是重写_ 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
最佳回答

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.

我认为,apache通过......ht access 相继, 无关紧要的下一个规则...

问题回答

暂无回答




相关问题
URL rewrite in IIS6 - Third party tools

I have collected the following api/sdk/whatever which provide modules for doing url rewriting in IIS 5/6/7. Some are free-open source and some require $. I haven t tried any of them. I would like to ...

Rewrite spring-security redirect URLs

I m trying to get Tuckey UrlRewriteFilter to tidy up URLs for my webapp. One problem I ve got is that when spring-security notices that an anonymous user is trying to access a protected resource it ...

ASP.NET Friendly URLs

In my research, I found 2 ways to do them. Both required modifications to the Application_BeginRequest procedure in the Global.Asax, where you would run your code to do the actual URL mapping (mine ...

Could I get by HttpModule the URL that contains * before?

My web-site is working under ASP.NET 3.5, IIS7, Integrated pipiline mode. My HttpModule is getting request to all resources, including URLs like this: http://my-site.com/address And I would support *...

Domains & Foreward Slash

This is rather difficult to explain so please bear with me. We will be hosting 4 websites on our server and the plan is to have each site sit under its own domain: site-a.com site-b.com sub1.site-b....

URL Mod-Rewrite

I currently have a working URL: http://example.com/security-services.php?service=fixed-camera-surveillance and then I have PHP say, $_REQUEST[ service ] to do some stuff... But I d like to ...

热门标签