gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 06:33:05 25 4
gpt4 key购买 nike

我有一个在 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 URL 重写模块? IIS Rewrite在上传新的 web.config 文件之前,是否需要将其安装在我的 Azure 托管网站上。
  2. 当用户输入 URL 时,如何才能从我的 URL 中删除 www。例如,如果用户输入 www.example.com,他们应该被重定向到 https://example.com。我想要这个的原因是因为在我的谷歌搜索控制台中我告诉谷歌将网址显示为 example.com 而不是 www.example.com
  3. 最后,这段代码能满足我的要求吗?有没有更专业的方法来实现这一目标?有什么好处。我应该指出,我的网站是 asp .net Web 表单。我知道 MVC 有路由选项,但这对我来说不是一个选项。

编辑:我不认为 How to force HTTPS using a web.config file解决了我的问题,因为我自己不托管 IIS,所以我什至不知道是否可以安装 URL 重写模块。 Azure 是否允许您访问 IIS 设置?我对 azure 的细节不熟悉。

最佳答案

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

关于azure - 除了 web.config 中编写的代码之外,我还需要什么才能将 azure 上托管的网站重定向到 https,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41894567/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com