配置仅对基础URL生效的IIS Rewrite规则(含HTTPS)
IIS Rewrite 规则配置方案
针对你的需求,以下是可避免重定向循环的IIS URL重写规则,直接放到站点根目录的web.config文件中即可:
<configuration> <system.webServer> <rewrite> <rules> <!-- 处理根路径请求,重定向到指定HTTPS路径 --> <rule name="Root Redirect to Target Path" stopProcessing="true"> <match url="^$" /> <conditions logicalGrouping="MatchAny"> <add input="{HTTP_HOST}" pattern="^abc\.123\.whatever$" /> <add input="{HTTP_HOST}" pattern="^www\.abc\.123\.whatever$" /> </conditions> <action type="Redirect" url="https://abc.123.whatever/bunchofstuff" redirectType="Permanent" /> </rule> <!-- 处理HTTP带路径请求,重定向到对应HTTPS路径 --> <rule name="HTTP Non-Root to HTTPS" stopProcessing="true"> <match url="(.*)" /> <conditions logicalGrouping="MatchAll"> <add input="{REQUEST_URI}" pattern="^/$" negate="true" /> <add input="{HTTPS}" pattern="off" /> <add input="{HTTP_HOST}" pattern="^(www\.)?abc\.123\.whatever$" /> </conditions> <action type="Redirect" url="https://abc.123.whatever/{R:1}" redirectType="Permanent" /> </rule> <!-- 处理HTTPS下www前缀带路径请求,重定向到裸域HTTPS路径 --> <rule name="HTTPS www to Non-www" stopProcessing="true"> <match url="(.*)" /> <conditions logicalGrouping="MatchAll"> <add input="{REQUEST_URI}" pattern="^/$" negate="true" /> <add input="{HTTPS}" pattern="on" /> <add input="{HTTP_HOST}" pattern="^www\.abc\.123\.whatever$" /> </conditions> <action type="Redirect" url="https://abc.123.whatever/{R:1}" redirectType="Permanent" /> </rule> </rules> </rewrite> </system.webServer> </configuration>
规则说明:
- 根路径规则:仅匹配域名后无任何路径的请求,不管原请求是http裸域、https裸域还是http www子域,都直接跳转到目标HTTPS路径,设置
stopProcessing="true"避免触发后续规则。 - HTTP带路径规则:仅针对非根路径的HTTP请求,重定向到对应路径的HTTPS裸域地址,保留原有URL路径。
- HTTPS www带路径规则:针对HTTPS下的www子域带路径请求,统一跳转至无www的HTTPS裸域对应路径,保证地址格式统一。
防循环关键:
- 所有规则通过匹配条件严格限制触发范围,不会对已经符合目标格式的请求重复重定向;
- 根路径规则只作用于空路径请求,不会影响
/bunchofstuff这个目标路径的正常访问; - 使用301永久重定向,既符合SEO需求,也减少浏览器不必要的重复请求。
内容的提问来源于stack exchange,提问作者user899402
相关产品推荐
相关产品推荐

