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

IIS 10(Win Server 2019)下React网站www重定向异常求助

解决React站点重定向时附带3000端口的问题

问题背景

我有一个运行在3000端口的React网站,当前带www与不带www均可访问,这会引发SEO重复内容问题,希望将所有请求统一重定向至https://www.pess.org.ua。

原web.config配置:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <rule name="ReverseProxyInboundRule1" stopProcessing="true">
                    <match url="(.*)" />
                    <action type="Rewrite" url="http://localhost:3000/{R:1}" />
                </rule>
                <rule name="RedirectToWWW" stopProcessing="true">
                    <match url="(.*)" />
                    <conditions logicalGrouping="MatchAny">
                        <add input="{HTTP_HOST}" pattern="^pess.org.ua$" />
                    </conditions>
                    <action type="Redirect" url="https://www.pess.org.ua/{R:1}" redirectType="Permanent" />
                </rule>
            </rules>
        </rewrite>
        <httpErrors>
            <remove statusCode="502" subStatusCode="-1" />
            <remove statusCode="404" subStatusCode="-1" />
            <error statusCode="404" prefixLanguageFilePath="" path="/errors/404.html" responseMode="ExecuteURL" />
            <error statusCode="502" prefixLanguageFilePath="" path="/errors/502.html" responseMode="ExecuteURL" />
        </httpErrors>
    </system.webServer>
</configuration>

访问http://pess.org.ua时,会被错误重定向至https://www.pess.org.ua:3000/,触发SSL错误:

此网站无法提供安全连接 www.pess.org.ua 发送了无效响应。
ERR_SSL_PROTOCOL_ERROR。

问题原因

当前web.config的规则顺序错误:反向代理规则排在最前面,会先把所有请求转发到本地3000端口,后续执行重定向规则时,响应会带上后端的3000端口,而域名实际在443端口提供HTTPS服务,因此出现SSL协议错误。

解决方法

调整规则执行顺序,先完成重定向(统一到https://www.pess.org.ua),再执行反向代理。同时补充HTTPS判断,确保所有HTTP请求也被重定向到HTTPS。

修正后的web.config:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <!-- 第一步:统一重定向到https://www.pess.org.ua -->
                <rule name="RedirectToWWWAndHTTPS" stopProcessing="true">
                    <match url="(.*)" />
                    <conditions logicalGrouping="MatchAny">
                        <!-- 匹配不带www的主域名 -->
                        <add input="{HTTP_HOST}" pattern="^pess.org.ua$" />
                        <!-- 匹配HTTP(非HTTPS)请求 -->
                        <add input="{HTTPS}" pattern="off" />
                    </conditions>
                    <action type="Redirect" url="https://www.pess.org.ua/{R:1}" redirectType="Permanent" />
                    <!-- 强制使用HTTPS默认端口,避免带上后端3000端口 -->
                    <serverVariables>
                        <set name="SERVER_PORT_SECURE" value="1" />
                    </serverVariables>
                </rule>
                
                <!-- 第二步:仅对已符合要求的请求执行反向代理 -->
                <rule name="ReverseProxyInboundRule1" stopProcessing="true">
                    <match url="(.*)" />
                    <conditions>
                        <add input="{HTTP_HOST}" pattern="^www.pess.org.ua$" />
                        <add input="{HTTPS}" pattern="on" />
                    </conditions>
                    <action type="Rewrite" url="http://localhost:3000/{R:1}" />
                </rule>
            </rules>
        </rewrite>
        <httpErrors>
            <remove statusCode="502" subStatusCode="-1" />
            <remove statusCode="404" subStatusCode="-1" />
            <error statusCode="404" prefixLanguageFilePath="" path="/errors/404.html" responseMode="ExecuteURL" />
            <error statusCode="502" prefixLanguageFilePath="" path="/errors/502.html" responseMode="ExecuteURL" />
        </httpErrors>
    </system.webServer>
</configuration>

核心调整点:

  • 调换规则顺序:先重定向再代理,确保重定向时不会带上后端端口。
  • 合并重定向规则:同时处理非www和非HTTPS的情况,减少重定向次数。
  • 代理规则加限制:只对已经是www+HTTPS的请求做代理,避免干扰重定向流程。
  • 设置SERVER_PORT_SECURE:明确告诉服务器用HTTPS默认端口443,防止响应里混入3000端口。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.14 16:15:57