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

同源双域名IIS HTTP重定向至非WWW HTTPS配置求助

解决多域名下HTTP转HTTPS+WWW转非WWW的重定向问题

直接用下面的规则替换你原有的rewrite配置,就能同时适配example.com和example.co两个域名,实现所有请求跳转到对应域名的非WWW HTTPS地址:

<rewrite>
  <rules>
    <rule name="Redirect to HTTPS non-www" stopProcessing="true">
      <match url="(.*)" />
      <conditions logicalGrouping="MatchAny">
        <!-- 匹配所有HTTP(非HTTPS)请求 -->
        <add input="{HTTPS}" pattern="^OFF$" />
        <!-- 匹配HTTPS但带www的目标域名 -->
        <add input="{HTTP_HOST}" pattern="^www\.(example\.com|example\.co)$" />
      </conditions>
      <!-- 跳转到对应域名的非WWW HTTPS地址 -->
      <action type="Redirect" url="https://{C:1}/{R:1}" redirectType="Permanent" />
      <!-- 如果用了反向代理(如Cloudflare),保留这段确保HTTPS检测正确 -->
      <serverVariables>
        <set name="HTTP_X_FORWARDED_PROTO" value="https" />
      </serverVariables>
    </rule>
  </rules>
</rewrite>

为什么原规则失效?

原规则的正则其实能捕获域名,但没有明确限定只匹配你的两个目标域名,如果服务器环境中存在默认域名配置,就可能导致{C:2}被错误替换成默认的.com域名。另外把两个规则合并成一个,逻辑更清晰,也避免了规则冲突。

关键细节说明

  • logicalGrouping="MatchAny":只要满足「HTTP请求」或「HTTPS带WWW」其中一个条件,就触发重定向
  • pattern="^www\.(example\.com|example\.co)$":精准匹配你的两个域名,避免对其他无关域名执行重定向,同时捕获example.com或example.co作为{C:1}
  • redirectType="Permanent":返回301永久重定向,利于SEO和浏览器缓存

验证清单

  1. 确认服务器已绑定两个域名的80(HTTP)和443(HTTPS)端口
  2. 两个域名都配置了有效的SSL证书
  3. 测试所有场景:
    • http://example.com → https://example.com
    • http://www.example.com → https://example.com
    • http://example.co → https://example.co
    • http://www.example.co → https://example.co
    • https://www.example.com → https://example.com
    • https://www.example.co → https://example.co

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.02 01:31:18