开发者

IIS make URL Rewrite greedy

开发者 https://www.devze.com 2023-01-26 22:26 出处:网络
I would like to make a generic URL Rewrite rule in IIS 7.5 (on Windows Web Server 2008 R2). I would like to match the following url\'s:

I would like to make a generic URL Rewrite rule in IIS 7.5 (on Windows Web Server 2008 R2).

I would like to match the following url's:

www.mysite.com/param

www.mysite.com/folder1/

www.mysite.com/folder1/param

www.mysite.com/folder1/folder2/

www.mysite.com/folder1/folder2/param

Notice the trailing slash (/) when I would like to match a folder, otherwise it is a parameter.

I have set up the following rewrite rule:

^(?:([^/]+)/)?(?:([^/]+)/)?([^/]+)?$

It has three matching clauses: {R:1}, {R:2} and {R:3}. However, when I input the following test-URL:

folder1/param

I get the following response:

{R:1} is empty

{R:2} = folder1

{R:3} = param

I suspected the following response:

{R:1} = folder1

{R:2} is empty

{R:3} = param

I.e. I want folder1 to be mapped to the first part of the rewrite pattern.

I would like to map the rewrite rule to:

/controller.php?folder1={R:1}&folder2={R:2}&param={R:3}
开发者_开发知识库

What am I missing to get the match to be greedy, i.e. match the first possible clause?


I don't know how to make the optional quantifier ("?") greedy in IIS. But I would try to make the match unambigous, by enclosing the second optional clause inside the first.

So instead of having the first two options side-by-side as in your original trial:

^(?:([^/]+)/)?(?:([^/]+)/)?([^/]+)?$

I suggest this:

^(?:([^/]+)/(?:([^/]+)/)?)?([^/]+)?$

Now the second group can only match if the first did match.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号