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

IIS重定向配置求助:实现example.com/example1路径定向

解决方案

你当前配置里的<httpRedirect>是全局重定向,会拦截所有请求直接跳转到Google,这就是/example1无法单独设置规则的核心原因。同时原HTTPS跳转规则直接跳转到Google的HTTPS,导致后续路径规则无法生效。以下是修正后的完整配置:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <!-- 强制HTTP跳转至当前域名的HTTPS版本 -->
                <rule name="Redirect to HTTPS" enabled="true" stopProcessing="true">
                    <match url="(.*)" />
                    <conditions>
                        <add input="{HTTPS}" pattern="^OFF$" />
                    </conditions>
                    <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Permanent" />
                </rule>

                <!-- 处理/example1及子路径的重定向 -->
                <rule name="Redirect example1 to Google" enabled="true" stopProcessing="true">
                    <match url="^example1(/.*)?$" />
                    <action type="Redirect" url="https://google.com" redirectType="Permanent" />
                </rule>

                <!-- 处理根域名的重定向 -->
                <rule name="Redirect root to Google" enabled="true" stopProcessing="true">
                    <match url="^$" />
                    <action type="Redirect" url="https://google.com" redirectType="Permanent" />
                </rule>
            </rules>
            <outboundRules>
                <rule name="Add Strict-Transport-Security when HTTPS" enabled="true">
                    <match serverVariable="RESPONSE_Strict_Transport_Security"
                        pattern=".*" />
                    <conditions>
                        <add input="{HTTPS}" pattern="on" ignoreCase="true" />
                    </conditions>
                    <action type="Rewrite" value="max-age=31536000; includeSubDomains; preload" />
                </rule>
            </outboundRules>
        </rewrite>
        <!-- 已注释全局重定向,避免干扰Rewrite规则 -->
        <!-- <httpRedirect enabled="true" destination="https://google.com" httpResponseStatus="Permanent" /> -->
    </system.webServer>
</configuration>

关键修改说明

  • 移除全局<httpRedirect>:该配置优先级高于Rewrite规则,会直接拦截所有请求跳转,必须注释或删除。
  • 调整HTTPS跳转目标:原规则直接跳转到Google的HTTPS,现在改为跳转到当前域名的HTTPS版本,确保后续路径规则能正常触发。
  • /example1专属规则:
    • ^example1(/.*)?$匹配/example1及所有子路径(如/example1/test),若只需匹配/example1本身,可改为^example1$。
    • stopProcessing="true"确保匹配到该路径后不再执行后续规则。
  • 根域名规则:用^$匹配空路径(即根域名),单独处理跳转逻辑。

验证步骤

  1. 替换原.web.config文件。
  2. 重启IIS站点或回收应用池。
  3. 测试访问:
    • http://example.com → 跳转至https://google.com
    • http://example.com/example1 → 先跳转HTTPS,再跳转至https://google.com
    • https://example.com/example1 → 直接跳转至https://google.com

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.06 09:07:42