【问题标题】:Azure Web App HTTP/HTTPS rewrite rulesAzure Web App HTTP/HTTPS 重写规则
【发布时间】:2016-12-29 08:05:44
【问题描述】:

我有一个托管在 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 应用程序并重新部署它,但它似乎没有启动,一切仍然重定向到 HTTPS,因为原始规则适用。

【问题讨论】:

    标签: azure redirect iis web-config http-redirect


    【解决方案1】:

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

    \/#\/user\/.+
    

    测试字符串:

    http://localhost/#/user/123
    

    经过测试的正则表达式:

    \/#\/user\/.+
    

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

    【讨论】:

    • 感谢您的回复,但不幸的是它没有工作..我在调试它时遇到了麻烦。知道如何调试吗?
    • 调试正则表达式?通过这个 regextester.com 没有帮助吗?
    • 是的,这很有帮助。我的意思是当我可以说正则表达式是正确的,但我仍然没有得到我想要的重定向。如何调试服务器上的实际重定向?这甚至可能吗?
    • 您可以使用诸如 Selenium 或 Coded UI 之类的 UI 自动化框架来自动化您的测试。我认为这是更简单的方法。此外,您可以在 IIS 上使用 URL 重写模块并通过它进行测试。 iis.net/learn/extensions/url-rewrite-module/…
    【解决方案2】:

    尝试将此添加到您的 Application_beginrequest 事件中

    if (!HttpContext.Current.Request.IsSecureConnection && !HttpContext.Current.Request.Url.Host.Contains("/#/user/[Some random user id]"))
            {
                Response.Redirect("https://" + Request.ServerVariables["HTTP_HOST"]
                                             + HttpContext.Current.Request.RawUrl);
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-06-02
      • 2012-02-04
      • 2012-12-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-12
      相关资源
      最近更新 更多