You need to enable JavaScript to run this app. IIS服务器URL Rewrite规则配置:多路径重写至index.html失败如何解决?
优惠活动
大模型
产品
解决方案
定价
更多

IIS服务器URL Rewrite规则配置:多路径重写至index.html失败如何解决?

问题原因

现有配置无法适配多级路径的核心问题有两个:

  • 重写动作的目标使用相对路径index.html,多级路径场景下IIS会将当前请求路径和重写目标拼接,例如请求test.com/test1/test2时会实际指向test.com/test1/test2/index.html而非根目录下的index.html
  • 仅排除了真实文件的匹配,未排除真实目录的匹配逻辑,可能被IIS静态目录处理规则拦截,导致重写不生效

修正后的完整web.config配置

<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <rule name="redirect all requests to root index.html" stopProcessing="true">
                    <match url="^(.*)$" ignoreCase="false" />
                    <conditions logicalGrouping="MatchAll">
                        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" pattern="" ignoreCase="false" />
                        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" pattern="" ignoreCase="false" />
                    </conditions>
                    <action type="Rewrite" url="/index.html" appendQueryString="true" />
                </rule>
            </rules>
        </rewrite>
    </system.webServer>
</configuration>

核心修改点:

  • 将重写目标从index.html改为/index.html,指定强制指向站点根目录下的index.html,不受当前请求路径层级影响
  • 新增排除真实目录的匹配条件,避免目录访问场景下重写规则被拦截

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.16 14:36:09