【问题标题】:IIS URL Rewrite rule to replace part of the URLIIS URL 重写规则以替换部分 URL
【发布时间】:2026-02-01 21:05:01
【问题描述】:

我是 IIS 重写规则的新手,并试图创建一个规则来替换部分 url

例如www.abc.com/assets/global/xyz.jpg

应该重定向到 www.abc.com**/en/globalassets/**assets/global/xyz.jpg

我摆弄了以下规则,但没有成功

<rule name="url replace">
<match url="^(.com/assets/global/)" />
<action type="Rewrite" url=".com/en/globalassets/assets/global/{R:2}" />
</rule>

【问题讨论】:

标签: iis url-rewriting url-rewrite-module


【解决方案1】:

根据你的描述,我已经测试过了,你可以使用如下的urlrewite规则:

<rule name="rule1" enabled="true" stopProcessing="true">
                    <match url="assets/global/(.*)" />
                    <conditions>
                        <add input="{REQUEST_URI}" pattern="en/globalassets" negate="true" />
                    </conditions>
                    <action type="Redirect" url="http://{domain}/en/globalassets/assets/global/{R:1}" />
                </rule>

首先,我们不能在匹配 url 中添加像 **.com 这样的值,因为这部分只能捕获 url 的路径。

你可以看到这是一个 url 结构:

http(s)://httphost/path?querystring.

你只能在条件标签中获得它,而不是模式。

然后你应该添加条件来检查请求 URL 是否匹配“en/globalassets”以避免一次又一次地运行重定向规则。

【讨论】: