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

IIS 10初始请求时跳转至政策确认页重写规则失效问题

IIS Rewrite 规则修正方案

当前配置的核心问题

  1. {REMOTE_HOST}不可靠:这个变量对应客户端主机名,绝大多数场景下IIS无法解析出完整主机名,只会返回客户端IP,导致你写的pattern="test.domain.com"根本匹配不到请求,规则自然不会触发。
  2. 未排除确认页本身:用户跳转到banner/banner-Popup.htm后,会再次触发规则,造成无限循环跳转。
  3. 硬编码域名:跳转URL里写死了https://test.domain.com,域名变更时需要修改规则,不如用{HTTP_HOST}动态获取当前请求域名。

修正后的重写规则

下面是调整后的规则,用更可靠的IP判断内部来源,同时避免循环跳转:

<rule name="Show Banner" stopProcessing="true">
    <!-- 匹配所有URL -->
    <match url="(.*)" />
    <conditions logicalGrouping="MatchAll">
        <!-- 排除内部IP请求,替换成你的实际内部IP段,多个用|分隔 -->
        <add input="{REMOTE_ADDR}" pattern="^127\.0\.0\.1$|^192\.168\.\d+\.\d+$" negate="true" />
        <!-- 排除确认页本身,防止循环 -->
        <add input="{URL}" pattern="^/banner/banner-Popup\.htm$" negate="true" />
        <!-- 可选:排除静态资源(图片/CSS/JS),不需要弹确认页 -->
        <add input="{URL}" pattern="\.(png|jpg|css|js|ico)$" negate="true" />
    </conditions>
    <!-- 动态拼接原URL,自动带上查询参数 -->
    <action type="Redirect" url="banner/banner-Popup.htm?u=https://{HTTP_HOST}{URL}{QUERY_STRING:?{QUERY_STRING}}" logRewrittenUrl="true" />
</rule>

确认页返回逻辑优化

为了符合DoD安全要求,要做URL解码和安全校验,防止开放重定向漏洞:

document.getElementById('accept-btn').addEventListener('click', function() {
    const urlParam = new URLSearchParams(window.location.search).get('u');
    if (!urlParam) {
        window.location.href = '/';
        return;
    }
    // 解码原URL
    const originalUrl = decodeURIComponent(urlParam);
    // 校验跳转目标为本站域名
    const allowedHost = window.location.host;
    const parsedUrl = new URL(originalUrl);
    if (parsedUrl.host === allowedHost) {
        window.location.href = originalUrl;
    } else {
        window.location.href = '/';
    }
});

额外说明

如果你的「本地来源」指的是请求HOST头不是test.domain.com(比如反向代理场景),可以把IP判断的条件替换为:

<add input="{HTTP_HOST}" pattern="test.domain.com" negate="true" />

注意要确保IIS能正确获取到HOST头(反向代理需配置传递Host头)。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.14 10:02:48