You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

IIS URL Rewrite规则配置异常:旧图片URL重定向路径错误

修正IIS URL Rewrite配置实现正确重定向

你的配置问题在于条件中的正则捕获组没有正确提取仅文件名部分,导致{C:1}包含了content/images/前缀,最终生成冗余路径。以下是两种修正方案:

方案1:调整条件正则的捕获组

修改条件中{URL}的正则,确保只捕获content/images/后的文件名:

<rewrite>
       <rules>
          <rule name="Rewrite Images to CDN" stopProcessing="true">
                <match url="(.*)" />
                <action type="Redirect" url="https://domain2.xyz.com/originalfiles/{C:1}" appendQueryString="true" redirectType="Permanent" />
                <conditions>
                   <add input="{HTTP_HOST}" pattern="domain1.xyz.com" />
                   <!-- 用^和$锚定路径,捕获组仅包含文件名 -->
                   <add input="{URL}" pattern="^content/images/(.*)$" />
                </conditions>
         </rule>
       </rules>
</rewrite>

方案2:简化逻辑,直接在Match规则中捕获文件名(推荐)

将路径匹配逻辑移到<match>标签中,仅保留主机名作为条件,逻辑更清晰:

<rewrite>
       <rules>
          <rule name="Rewrite Images to CDN" stopProcessing="true">
                <!-- 直接匹配content/images/开头的路径,捕获后面的文件名 -->
                <match url="^content/images/(.*)$" />
                <!-- 使用{R:1}引用Match规则中捕获的文件名 -->
                <action type="Redirect" url="https://domain2.xyz.com/originalfiles/{R:1}" appendQueryString="true" redirectType="Permanent" />
                <conditions>
                   <add input="{HTTP_HOST}" pattern="domain1.xyz.com" />
                </conditions>
         </rule>
       </rules>
</rewrite>

两种方案都能将旧URLhttps://domain1.xyz.com/content/images/[文件名]正确重定向到https://domain2.xyz.com/originalfiles/[文件名],方案2更简洁易维护。

内容的提问来源于stack exchange,提问作者Naupad Doshi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.10 04:20:18