如何在IIS的web.config现有重定向规则中添加旧域名转新域名重定向?
修正旧域名跳转规则的方案
你的旧规则失效的核心问题有三个:
- 错误地用
<match url>匹配域名,而url节点对应的是域名之后的路径部分,不是域名本身; - 未设置
stopProcessing="true",导致后续规则会继续执行,打乱跳转逻辑; - 没覆盖带
www前缀的旧域名请求(比如www.olddomain.com/xxx)。
修正后的规则代码
把以下规则放在「Redirect to https」规则之后、「Redirect - Top domains with non-www to www」规则之前:
<rule name="Old to new domain" enabled="true" stopProcessing="true"> <match url="*" /> <conditions logicalGrouping="MatchAny"> <add input="{HTTP_HOST}" pattern="^olddomain\.com$" /> <add input="{HTTP_HOST}" pattern="^www\.olddomain\.com$" /> </conditions> <action type="Redirect" url="https://www.newdomain.com" redirectType="Permanent" /> </rule>
规则说明
- 匹配所有路径:
<match url="*"会捕获旧域名下的任意请求路径(不管是根目录还是/whatever); - 覆盖所有旧域名变体:通过
{HTTP_HOST}条件匹配olddomain.com和www.olddomain.com两种域名形式; - 终止后续规则:
stopProcessing="true"确保完成跳转后不再执行后面的www前缀、尾斜杠规则; - 301永久重定向:
redirectType="Permanent"是搜索引擎友好的重定向类型,可传递权重。
最终规则顺序
调整后的完整规则顺序应该是:
- Redirect to https
- Old to new domain(新增的修正规则)
- Redirect - Top domains with non-www to www
- Add trailing slash
内容的提问来源于stack exchange,提问作者Dan
相关产品推荐
相关产品推荐

