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

时间:2017-01-27 13:00:27

标签: azure redirect

我有一个我在azure上托管的网站。我最近购买了SSL并对其进行了配置。现在,用户可以通过键入http://example.comhttps://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文件之前,我有几个问题。

  1. 什么是IIS网址重写模块? IIS Rewrite并且在我上传新的web.config文件之前,需要在我的Azure托管网站上安装它。
  2. 如何在用户输入时从我的网址中删除www。例如,如果用户输入www.example.com,则应将其重定向到https://example.com。我想要这个的原因是因为在我的谷歌搜索控制台中,我已经告诉谷歌将网址显示为example.com而不是www.example.com
  3. 最后,这段代码会做我想要的吗?有没有更专业的方法来实现这一目标?有什么好处。我应该注意,我的网站是asp .net网络表单。我知道MVC有路由选项,但对我来说这不是一个选择。
  4. 编辑:我不认为How to force HTTPS using a web.config file解决了我的问题,因为我甚至不知道我是否可以安装网址重写模块,因为我自己没有托管IIS。 azure是否允许您访问IIS设置?我不熟悉天蓝色的细节。

2 个答案:

答案 0 :(得分:3)

Microsoft URL Rewrite Module for IIS使IIS管理员能够创建功能强大的自定义规则,将请求URL映射到友好的URL,方便用户记住,以便搜索引擎更容易找到。

此模块已预安装到Azure Web App,如检查Kudu中Azure Web App的 applicationHost.config 时所示。

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

URL Rewrite Module in Azure Web App

用于强制Azure Web应用程序的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>

答案 1 :(得分:0)

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

更多示例https://github.com/projectkudu/kudu/wiki/Xdt-transform-samples

    <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>