简体   繁体   English

IIS 使用 Url 路径段重写规则

[英]IIS Rewrite Rule using Url Path segments

I'm trying to write, hopefully, a single rewrite rule for a number of URLs that are being moved to a different structure.希望我正在尝试为许多正在移动到不同结构的 URL 编写一个重写规则。 All of the URLs have a very similar structure so I'm thinking there must be a way to write a single rule for all of them.所有的 URL 都具有非常相似的结构,所以我认为必须有一种方法可以为所有这些 URL 编写一个规则。

Example:例子:

http://example.com/some/deep/path/a/001/my-a-data
http://example.com/some/deep/path/a/002/my-a-data2
http://example.com/some/deep/path/b/001/my-b-data
http://example.com/some/deep/path/b/002/my-b-data2
http://example.com/some/deep/path/c/001/my-c-data
http://example.com/some/deep/path/c/002/my-c-data2

Need to redirect to (respectively):需要重定向到(分别):

http://example.com/a/my-a-data
http://example.com/a/my-a-data2
http://example.com/b/my-b-data
http://example.com/b/my-b-data2
http://example.com/c/my-c-data
http://example.com/c/my-c-data2

So what I'm trying to know is if there is a way to read the segments of the PATH in the URL so I can just reuse them to build the final URL because it is all there.所以我想知道的是是否有办法读取 URL 中的 PATH 段,这样我就可以重用它们来构建最终的 URL,因为它就在那里。 It is just a simplification of the URL.它只是 URL 的简化版。

Perhaps to do something like也许做类似的事情

                  /{1}/{2}/{3}/{4}/{5}/{6}
http://example.com/some/deep/path/a/001/my-a-data  ->  http://example.com/{4}/{6}

Is that possible?那可能吗?

Thanks!!!谢谢!!!

It is recommended to use([^/])+ instead of (.*).建议使用 ([^/])+ 而不是 (.*)。 If there are multiple slash in your URL, the capture group would be corrupted.如果 URL 中有多个斜线,则捕获组将损坏。

For example:例如:

在此处输入图像描述

在此处输入图像描述

Best regards,此致,

Sam山姆

I found how it works.我发现它是如何工作的。

Each segment in the URL path can be identified with {R} parameters in the rewrite rules. URL 路径中的每个段都可以用重写规则中的 {R} 参数来标识。 To match my URL structure I created the following rule:为了匹配我的 URL 结构,我创建了以下规则:

<rule name="Documents redirection" stopProcessing="true">
  <match url="^some/deep/path/(.*)/(.*)/(.*)$" ignoreCase="true" />
  <action type="Redirect" url="/{R:1}/{R:3}" redirectType="Permanent" />
</rule>

Each of the (.*) segments are what the IIS Rewrite module recognizes as {R} parameter, so for my need, I needed the first and third parameter to form a new URL.每个 (.*) 段都是 IIS 重写模块识别为 {R} 参数的内容,因此根据我的需要,我需要第一个和第三个参数来形成一个新的 URL。 I can safely ignore the second parameter.我可以放心地忽略第二个参数。

I hope this helps someone else.我希望这对其他人有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM