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

IIS 10单域名访问时URL重定向失败问题求助

问题描述

我管理matiasmasso.es和matiasmasso.pt两个域名,在Web.Config中配置了重定向规则,大部分URL都能正常生效,但存在以下例外情况:

  • 访问http://www.matiasmasso.pt(无SSL触发规则)时,会错误重定向至https://www.matiasmasso.es;
  • 访问带路径的URL如http://www.matiasmasso.pt/test时,能成功跳转至https://www.matiasmasso.pt/test。

当前使用的重定向规则代码如下:

<rewrite>
  <rules>
      
      <rule name="RedirectToHTTPS" stopProcessing="true">
          <match url="(.*)" />
          <conditions>
              <add input="{HTTPS}" pattern="off" ignoreCase="true" />
          </conditions>
          <action type="Redirect" url="https://{SERVER_NAME}/{R:1}" redirectType="Permanent" />
      </rule>

      <rule name="RedirectNonWwwEsToWww" stopProcessing="true">
          <match url="(.*)" />
          <conditions>
              <add input="{HTTP_HOST}" pattern="^matiasmasso.es$"  ignoreCase="true"/>
          </conditions>
          <action type="Redirect" url="http://www.matiasmasso.es/{R:0}" redirectType="Permanent" />
      </rule>

      <rule name="RedirectNonWwwPtToWww" stopProcessing="true">
          <match url="(.*)" />
          <conditions>
              <add input="{HTTP_HOST}" pattern="^matiasmasso.pt$"  ignoreCase="true"/>
          </conditions>
          <action type="Redirect" url="http://www.matiasmasso.pt/{R:0}" redirectType="Permanent" />
      </rule>
      
  </rules>
</rewrite>
解决建议

问题根源在于RedirectToHTTPS规则中使用的{SERVER_NAME}变量。当访问根路径http://www.matiasmasso.pt时,IIS的SERVER_NAME可能被站点绑定的默认域名覆盖为matiasmasso.es,导致重定向目标错误;而带路径的请求因为{R:1}存在值,服务器处理时SERVER_NAME能正确匹配当前访问的域名,所以跳转正常。

核心修复步骤

将RedirectToHTTPS规则里的{SERVER_NAME}替换为{HTTP_HOST},{HTTP_HOST}会直接读取请求的主机头信息,能准确对应当前访问的域名:

<rule name="RedirectToHTTPS" stopProcessing="true">
    <match url="(.*)" />
    <conditions>
        <add input="{HTTPS}" pattern="off" ignoreCase="true" />
    </conditions>
    <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Permanent" />
</rule>

优化建议

当前的非www转www规则跳转目标是HTTP协议,之后会被HTTPS规则再次重定向,增加了一次跳转请求。建议直接将这两个规则的跳转目标改为HTTPS,提升用户体验:

<rule name="RedirectNonWwwEsToWww" stopProcessing="true">
    <match url="(.*)" />
    <conditions>
        <add input="{HTTP_HOST}" pattern="^matiasmasso.es$"  ignoreCase="true"/>
    </conditions>
    <action type="Redirect" url="https://www.matiasmasso.es/{R:0}" redirectType="Permanent" />
</rule>

<rule name="RedirectNonWwwPtToWww" stopProcessing="true">
    <match url="(.*)" />
    <conditions>
        <add input="{HTTP_HOST}" pattern="^matiasmasso.pt$"  ignoreCase="true"/>
    </conditions>
    <action type="Redirect" url="https://www.matiasmasso.pt/{R:0}" redirectType="Permanent" />
</rule>

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.16 07:40:21