除了web.config中编写的代码之外,我还需要什么来将azure上托管的网站重定向到https吗

除了web.config中编写的代码之外,我还需要什么来将azure上托管的网站重定向到https吗,azure,redirect,Azure,Redirect,我有一个我在azure上托管的网站。我最近买了一个SSL并对其进行了配置。现在用户可以通过键入http://example.com或https://example.com 我希望输入前者的用户能够自动重定向到后者,同时在.com之后保留任何内容 因此,如果用户键入http://example.com/about它们将被重定向到https://example.com/about 经过一些阅读,我发现这段代码似乎满足了我的需求 <system.webServer> <rewrite

我有一个我在azure上托管的网站。我最近买了一个SSL并对其进行了配置。现在用户可以通过键入
http://example.com
https://example.com

我希望输入前者的用户能够自动重定向到后者,同时在.com之后保留任何内容

因此,如果用户键入
http://example.com/about
它们将被重定向到
https://example.com/about

经过一些阅读,我发现这段代码似乎满足了我的需求

<system.webServer>
<rewrite>
<rules>
<rule name=”Redirect to https”>
<match url=”(.*)”/>
<conditions>
<add input=”{HTTPS}” pattern=”Off”/>
<add input=”{REQUEST_METHOD}” pattern=”^get$|^head$” />
</conditions>
<action type=”Redirect” url=”https://{HTTP_HOST}/{R:1}”/>
</rule>
</rules>
</rewrite>
</system.webServer>

在我上传新的web.config文件之前,它是否需要安装在我的azure托管网站上
  • 当用户输入www时,如何从我的URL中删除www。例如,如果用户在
    www.example.com
    中键入,则应将其重定向到
    https://example.com
    。我之所以想这样做,是因为在我的谷歌搜索控制台中,我告诉谷歌将URL显示为
    example.com
    ,而不是
    www.example.com
  • 最后,这段代码能满足我的要求吗?有没有更专业的方法来实现这一点?好处是什么。我应该注意,我的网站是asp.NETWeb表单。我知道MVC有路由选择,但这不是我的选择 编辑:我不认为解决了我的问题,因为我甚至不知道我是否可以安装URL重写模块,因为我自己没有托管IIS。azure是否允许您访问IIS设置?我不熟悉azure的细节

    Microsoft使IIS管理员能够创建功能强大的自定义规则,将请求URL映射到用户易于记住且搜索引擎易于查找的友好URL

    此模块是为Azure Web App预安装的,如在Kudu中检查Azure Web App的applicationHost.config时所示

    因此,您无需担心Azure Web App模块的可用性

    为Azure web app强制执行HTTPS重定向的URL重写配置是实现所需的最简单方法。您的上述配置将适用 仅当请求方法为HTTP GET或HTTP HEAD时。以下配置将不具有此类限制

    <system.webServer>
        <rewrite>
            <rules>
                <rule name="Force HTTPS Redirection" enabled="true" stopProcessing="true">
                    <match url="^$" ignoreCase="false"/>
                    <conditions>
                        <add input="{HTTPS}" pattern="^OFF$"/>
                    </conditions>
                    <action type="Redirect" url="https://{HTTP_HOST}/" redirectType="Permanent"/>
                </rule>
            </rules>
        </rewrite>
    </system.webServer>
    

    我想补充最后一件事。假设您在Azure Web应用程序上运行,它们会对您的站点进行各种探测以进行预热和初始化。您可能不希望这些探测也被重定向,否则,当您重新启动或使用Azure的交换功能进行蓝/绿部署时,您可能会遇到一些问题。然后,这些探测将以301/302返回,而不是实际访问您的站点(而且Azure实际上没有遵循重定向)

    更多例子


    提示:不要购买SSL。而是使用免费的LetsEncrypt服务。
        <rule name="Redirect to non-WWW" stopProcessing="true">
          <match url="(.*)" />
          <conditions>
            <add input="{HTTP_HOST}" pattern="www.example.com$" />
            <add input="{HTTP_USER_AGENT}" pattern="Initialization" negate="true" /> <!-- IIS Application Initialization Warmup -->
            <add input="{HTTP_USER_AGENT}" pattern="SiteWarmup" negate="true" /> <!-- Azure WebApps Warmup Request -->
            <add input="{HTTP_USER_AGENT}" pattern="AlwaysOn" negate="true" /> <!-- Azure WebApps AlwaysOn Probes -->
          </conditions>
          <action type="Redirect" redirectType="Permanent" url="https://example.com/{R:1}" />
        </rule>
    
        <!-- Redirect to HTTPS Version -->
        <rule name="HTTP to HTTPS redirect" stopProcessing="true">
          <match url="(.*)" />
          <conditions>
            <add input="{HTTPS}" pattern="off" ignoreCase="true" />
            <add input="{HTTP_USER_AGENT}" pattern="Initialization" negate="true" />
            <add input="{HTTP_USER_AGENT}" pattern="SiteWarmup" negate="true" />
            <add input="{HTTP_USER_AGENT}" pattern="AlwaysOn" negate="true" />
          </conditions>
          <action type="Redirect" redirectType="Permanent" url="https://{HTTP_HOST}/{R:1}" />
        </rule>