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

IIS指定URL和端口的Rewrite规则不生效问题排查

IIS Rewrite规则配置问题(ASP.NET MVC项目)

我正在为特定网站和端口配置IIS Rewrite规则(基于ASP.NET MVC项目),访问http://127.0.0.1时预期跳转到Google,但规则未生效,详情如下:

初始未生效的配置

web.config中的配置:

<rewrite>
    <rules>
        <rule name="test" stopProcessing="true">
            <match url="^(https?:\/\/)?127\.0\.0\.1:80\/?$" />
            <conditions>
            </conditions>
            <action type="Redirect" url="http://google.com" />
        </rule>
    </rules>
</rewrite>

修改后生效的配置

将匹配规则改为(.*)后,规则正常生效:

<rewrite>
    <rules>
        <rule name="test" stopProcessing="true">
            <match url="(.*)" />
            <conditions>
            </conditions>
            <action type="Redirect" url="http://google.com" />
        </rule>
    </rules>
</rewrite>

入站规则测试情况

初始的匹配模式在入站规则测试中显示完全有效。

添加条件后仍未解决

尝试添加条件,问题依旧:

<rewrite>
    <rules>
        <rule name="test" stopProcessing="true">
            <match url="^(https?:\/\/)?127\.0\.0\.1:80\/?$" />
            <conditions>
                <add input="{HTTP_HOST}" pattern="^(https?:\/\/)?127\.0\.0\.1:80\/?$" />
            </conditions>
            <action type="Redirect" url="http://google.com" />
        </rule>
    </rules>
</rewrite>

FRT失败请求跟踪日志

启用FRT后获取的日志内容:

<failedRequest url="http://127.0.0.1:80/" siteId="1" appPoolId="DefaultAppPool" 
processId="8432" verb="GET" remoteUserName="" userName="" tokenUserName="NT AUTHORITY\IUSR" 
authenticationType="anonymous" activityId="{800008BA-0001-E700-B63F-84710C7967BB}" 
failureReason="STATUS_CODE" statusCode="302" triggerStatusCode="302" timeTaken="0" 
xmlns:freb="http://schemas.microsoft.com/win/2006/06/iis/freb">

问题原因与解决方案

核心问题是IIS Rewrite的<match url>仅匹配请求路径部分,不包含协议、域名和端口,你初始配置把完整URL写在match url中,自然无法匹配。

正确配置需拆分匹配逻辑:用{HTTP_HOST}匹配域名和端口,match url匹配根路径:

<rewrite>
    <rules>
        <rule name="test" stopProcessing="true">
            <match url="^\/?$" /> <!-- 匹配根路径(空或带/的情况) -->
            <conditions>
                <add input="{HTTP_HOST}" pattern="^127\.0\.0\.1:80$" /> <!-- 匹配主机IP和端口 -->
            </conditions>
            <action type="Redirect" url="http://google.com" />
        </rule>
    </rules>
</rewrite>

另外,FRT日志显示状态码302,说明规则曾触发重定向,若仍有异常可检查浏览器缓存或其他规则干扰。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.19 02:20:36