【问题标题】:How can I exclude specific subdomains from redirect to HTTPS in web.config?如何在 web.config 中将特定子域从重定向到 HTTPS 中排除?
【发布时间】:2021-12-28 13:08:55
【问题描述】:

我正在开发一个 MVC 项目,我的 web.config 中有以下工作规则,但它会将所有子域重定向到 https。我想做的是排除一些特定的子域。例如,我需要重定向“mywebsite.com”和“www.mywebsite.com”,而不是“test.mywebsite.com”或“beta.mywebsite.com”。排除的子域应保留为“http://test.mywebsite.com”和“http://beta.mywebsite.com”,而不被重定向到 https。我怎样才能做到这一点?这是我在 web.config 中的规则:

<rule name="Redirect to https" stopProcessing="true">
  <match url=".*" />
  <conditions>
    <add input="{HTTPS}" pattern="off" ignoreCase="true" />
    <add input="{REQUEST_URI}" negate="true" pattern="^/\.well-known/pki-validation/(.*)$" ignoreCase="true" />
  </conditions>
  <action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent" appendQueryString="true" />
</rule>

【问题讨论】:

    标签: redirect https web-config


    【解决方案1】:

    我曾经有一个类似的配置曾经使用过这样的规则:

    <?xml version="1.0" encoding="UTF-8"?>
    <configuration>
        <system.webServer>
            <rewrite>
                <rules>
                    <rule name="redirect to https2" stopProcessing="true">
                        <match url="^" ignoreCase="false" />
                        <conditions logicalGrouping="MatchAll">
                            <add input="{HTTP_HOST}" pattern="^subdomain\.example\.com$" negate="true" />
                            <add input="{HTTPS}" pattern="off" ignoreCase="false" />
                        </conditions>
                        <action type="Redirect" url="https://{HTTP_HOST}{URL}" redirectType="Found" appendQueryString="true" />
                    </rule>
                </rules>
            </rewrite>
        </system.webServer>
    </configuration>
    

    【讨论】:

    • 感谢您为我指明正确的方向。我已将条件更改为仅包含主域和 www,它起作用了:``` ```
    猜你喜欢
    • 1970-01-01
    • 2016-06-06
    • 2018-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多