我正在尝试制定一个 IIS 重定向规则来从这个 url 模式重定向,但它打败了我:
https://www.mycompanyPLC.com/en/lorem/ipsum/whatever
至
https://www.mycompanyLTD.com/lorem/ipsum/whatever
基本上我需要用 LTD 替换 PLC,如果 url 中有“/en/”组,则必须将其删除。
我正在尝试制定一个 IIS 重定向规则来从这个 url 模式重定向,但它打败了我:
https://www.mycompanyPLC.com/en/lorem/ipsum/whatever
至
https://www.mycompanyLTD.com/lorem/ipsum/whatever
基本上我需要用 LTD 替换 PLC,如果 url 中有“/en/”组,则必须将其删除。
/en/您可以使用前面提供的单个正则表达式来满足您的两个要求.com。就像是:
(.*?)PLC\.com(?:\/\ben\b)?(.*)
上述正则表达式的解释:
(.*?)- 代表 1st 捕获组在PLC lazily之前捕获所有内容。PLC\.comPLC.com-从字面上匹配。(?:\/\ben\b)?- 表示一个非捕获组匹配\en字面上零或一次。\b表示单词边界。(.*)\en - 表示贪婪之后匹配所有内容的第二个捕获组。$1LTD.com$2- 对于替换(或本例中的重定向)部分,您可以使用此字符串,其中$1代表第一个捕获的组并$2代表第二个捕获的组。在你的情况下;你可以使用{R:1}LTD.com{R:2}.您可以在此处找到上述正则表达式的演示。
请参考以下 URL 规则。
<system.webServer>
<rewrite>
<rules>
<rule name="ReverseProxyInboundRule1" stopProcessing="true">
<match url="en(.*)" />
<action type="Redirect" url="https://www.mycompanyLTD.com{R:1}" />
</rule>
</rules>
</rewrite>
</system.webServer>
无需/en强制匹配 URL 片段。只要我们发现我们有一个/enURL 段,我们就会重定向请求。http/httpsURL 段也是如此。
如果有什么我可以帮忙的,请随时告诉我。
经过几个小时的 Regex 讲座,我创建了这个规则并且似乎正在工作(我已经测试了几个场景):
^(http|https)://?(www.)mycompanyPLC.com/en?(.*)
来自 IIS 的重定向 URL 是:
https://www.mycompanyLTD.com/{R:3}
后期编辑:
IIS中的规则是这样的:
<rule name="Replace PLC with LTD and remove /en/" enabled="true" stopProcessing="true">
<match url="(.*?)PLC\.com(?:\/\ben\b)?(.*)" />
<conditions logicalGrouping="MatchAny" trackAllCaptures="false" />
<action type="Redirect" url="{R:1}ltd.com{R:2}" />
</rule>
测试网址是这种格式:
http://webdev.myCompanyplc.com/en/our-experience/retail
{R:1} = http://webdev.myCompany
{R:2} = /our-experience/retail
正则表达式没问题,但重定向仍然不起作用