Azure Web App HTTP/HTTPS 重写规则

标签 azure redirect iis web-config http-redirect

我有一个在 Microsoft Azure 网站中托管的 Angular 应用程序。我已经通过为我安装扩展来将所有请求自动重定向到 HTTPS:https://github.com/gregjhogan/redirect-http-to-https-site-extension

扩展应用程序的作用是添加一个 applicationhost.xdt 文件,如下所示:

<?xml version="1.0" ?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <location path="%XDT_SITENAME%" xdt:Transform="InsertIfMissing" xdt:Locator="Match(path)">
    <system.webServer xdt:Transform="InsertIfMissing">
      <rewrite xdt:Transform="InsertIfMissing">
        <rules xdt:Transform="InsertIfMissing" lockElements="clear">
          <rule name="redirect HTTP to HTTPS" enabled="true" stopProcessing="true" lockItem="true">
            <match url="(.*)" />
            <conditions>
              <add input="{HTTPS}" pattern="off" ignoreCase="true" />
              <add input="{WARMUP_REQUEST}" pattern="1" negate="true" />
            </conditions>
            <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" appendQueryString="true" redirectType="Permanent" />
          </rule>
        </rules>
      </rewrite>
    </system.webServer>
  </location>
</configuration>

现在工作正常,所有内容都重定向到 HTTPS。现在我遇到了一个新要求,我想将重写规则编辑为以下内容:将所有请求重定向到 HTTPS(就像现在一样),除了包含以下内容的任何 url:“/#/user/[Some随机用户 ID]”。

我读过不同的帖子,并尝试了一些不同的东西,但 IIS 设置和类似的东西不完全是我的领域,我正在努力寻找它。当我添加此 {REQUEST_URI} 条件时,我感觉已经很接近了:

<conditions>
  <add input="{HTTPS}" pattern="off" ignoreCase="true" />
  <add input="{REQUEST_URI}" negate="true" pattern=".*/#/user/.+" ignoreCase="true" />
  <add input="{WARMUP_REQUEST}" pattern="1" negate="true" />
</conditions>

我尝试重新启动 Azure Web App,并重新部署它,但它似乎没有启动,所有内容仍然重定向到 HTTPS,因为原始规则适用。

最佳答案

我正在在线测试你的正则表达式,看来你需要转义正斜杠:

\/#\/user\/.+

测试字符串:

http://localhost/#/user/123

测试的正则表达式:

\/#\/user\/.+

网站: http://www.regextester.com/

关于Azure Web App HTTP/HTTPS 重写规则,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39085164/

相关文章:

python - 在 Python 中访问 Azure Key Vault 时出现意外的关键字参数 'tenant_id'

sql - 如何在SQL DW中使用Rollup Grouping Function报错?

html - 将目录重定向到目录中的文件

ssl - 在 IIS 上设置多个站点并添加 SSL - GCP 虚拟机

asp.net - HTTP 错误 500.19 和错误代码 : 0x80070021 while configuring IIS server

azure - 从 Azure CLI 创建日志警报

python - 函数未显示在 Azure Function App 中

c# - 在 IIS 中运行时找不到 .dll.config 文件

c# - 如何检测 Console.In (stdin) 是否被重定向?

php - 使用基于引荐来源网址的 htaccess 重定向