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
相关产品推荐
相关产品推荐

