asp.net - web.config 将非 www 或非 https 重定向到 https ://www using rules

标签 asp.net redirect web-config

我需要使用 web.config 中的规则将所有非 www 或非 https 流量重定向到 https://www

http://domain.com --> https://www.domain.com
http://www.domain.com --> https://www.domain.com
https://domain.com --> https://www.domain.com

我修改了规则 Web.config. Redirect all traffic to www.my... Using rules element.但它无法将 http://www.domain.com 重定向到 https://www.domain.com

    <rewrite>
        <rules>
            <clear />
            <rule name="Redirect to www subdomain">
                <match url=".*" />
                <conditions logicalGrouping="MatchAll">
                    <add input="{HTTP_HOST}" pattern="^(www\.)(.*)$" negate="true" />
                    <add input="{SERVER_PROTOCOL}" pattern="^(.*)(/.*)?$"/>
                </conditions>
                <action type="Redirect" url="https://www.{HTTP_HOST}/{R:0}" redirectType="Permanent"/>
            </rule>
        </rules>
    </rewrite>

最佳答案

我找到了答案,我认为如果我将其发布在这里,会对其他人有所帮助。

    <rewrite>
        <rules>
            <clear />
            <rule name="Redirect non-www OR non-https to https://www">
                <match url=".*" />
                <conditions logicalGrouping="MatchAny">
                    <add input="{HTTP_HOST}" pattern="^domain.com$" />
                    <add input="{HTTPS}" pattern="off" />
                </conditions>
                <action type="Redirect" url="https://www.domain.com/{R:0}" redirectType="Permanent"/>
            </rule>
        </rules>
    </rewrite>

关于asp.net - web.config 将非 www 或非 https 重定向到 https ://www using rules,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29112199/

相关文章:

c# - 应该向有经验的 OOP 程序员推荐什么脚本语言?

c# - 将字符串数组传递给 webservice 方法

asp.net - 为什么 ServiceModel 有时在 Framework 文件夹的 Web.config 中声明,有时则不然?

azure - 如何在Azure Web角色项目中设置覆盖不同环境的redis主机名?

c# - 具有 MVC 2.0 Web 配置的自定义角色提供程序

c# - 在 cors 主机中设置时,是否需要在 C# 中定义协议(protocol)

ASP.NET 4.0 URL重写: How to deal with the IDs

Google Cloud Run 上的重定向

c# - 带有重定向的代码隐藏 JavaScript 警报

linux - 将流量重定向到请求特定网页的另一台本地计算机