【问题标题】:Host Angular app on IIS - Redirect to root and force HTTPS在 IIS 上托管 Angular 应用程序 - 重定向到 root 并强制使用 HTTPS
【发布时间】:2018-07-22 05:27:05
【问题描述】:

我在 IIS 中托管了一个 Angular 应用程序。为了将所有请求重定向到应用程序的根目录并允许角度路由处理不同的路由,我添加了一个带有 URL Rewrite 规则的 web.config 文件,如下所示:

<configuration>
<system.webServer>
  <rewrite>
    <rules>
     <rule name="Angular Routes" stopProcessing="true">
         <match url=".*" />
       <conditions logicalGrouping="MatchAll">
         <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
        <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
       </conditions>
       <action type="Rewrite" url="/" />
     </rule>
  </rules>
 </rewrite>
</system.webServer>
</configuration>

它工作正常。但是,我现在需要强制应用程序从 HTTP 重定向到 HTTPS。为此,我可以添加一条新规则,如下所示:Angular 2 - always redirect to https:// instead of using http://

强制使用 HTTPS 的规则:

<configuration>
<system.webServer>
    <rewrite>
        <rules>
            <clear />
            <rule name="Redirect to https" stopProcessing="true">
                <match url="(.*)" />
                <conditions>
                    <add input="{HTTPS}" pattern="off" ignoreCase="true" />
                </conditions>
                <action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent" appendQueryString="false" />
            </rule>
        </rules>
    </rewrite>
</system.webServer>
</configuration>

此规则也有效,但它破坏了我之前定义的重定向到根规则。那么有没有人知道如何将这两个规则混合在一起,或者我可以通过哪些其他方式来实现这两个目的?

【问题讨论】:

  • 我遇到了同样的问题..你能解决吗?
  • 我最终用几行代码直接在 Angular 项目中强制使用 https。如果您有兴趣做同样的事情,请告诉我,我将与您分享我的代码。
  • 当然.. 这对我很有帮助。我的项目在 Angular 5.3.2 中,API 项目在 dot net core 2.1.3 中。谢谢。
  • 当然,我发布了替代解决方案作为答案

标签: angular redirect iis https url-rewrite-module


【解决方案1】:

作为替代解决方案,我最终直接在 Angular 项目的主控制器上强制使用 https,如下所示。 (张贴以防万一对其他人有用)

import { Component, OnInit } from '@angular/core';
import { Location } from '@angular/common';
import { environment } from '../environments/environment';

@Component({
  selector: 'vp-app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss']
})
export class AppComponent implements OnInit {
  title = 'vp-app';
  location: Location;

  ngOnInit() {
    if (environment.production) {
      if (location.protocol === 'http:') {
        window.location.href = location.href.replace('http', 'https');
      }
    }
  }
}

【讨论】:

  • 在重定向到身份提供者之前会发生这种情况吗?
  • 只在第一个链接上为我工作,在所有其他链接上,它是 HTTP
  • 它适用于我,例如,当我尝试访问 http://localhost:5000/products 时,它会自动重定向到 https://localhost:5000/products
【解决方案2】:

重定向到 HTTPS 并重定向到根目录

仍在寻找 web.config 解决方案的人,将您现有的 web.config 文件代码替换为以下代码:

<?xml version="1.0" encoding="UTF-8"?>
  <configuration>
   <system.webServer>
    <rewrite>
      <rules>

          <!--START REDIRECT TO HTTPS-->
           <rule name="Redirect to https" stopProcessing="true">
                <match url=".*" />
                <conditions>
                    <add input="{HTTPS}" pattern="off" ignoreCase="true" />
                </conditions>
                <action type="Redirect" url="https://{HTTP_HOST}{REQUEST_URI}" redirectType="Permanent" appendQueryString="false" />
            </rule>
            <!--END REDIRECT TO HTTPS-->

            <!--START REDIRECT TO ROOT-->
            <rule name="AngularJS" stopProcessing="true">
              <match url=".*" />
               <conditions logicalGrouping="MatchAll">
                  <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                  <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
               </conditions>
               <action type="Rewrite" url="/" />
            </rule>
           <!--END REDIRECT TO ROOT-->

         </rules>
      </rewrite>
    </system.webServer>
 </configuration>

注意: 对于 IIS 6,您可能需要安装 URL Rewrite 扩展才能使其工作。

你可以在https://www.iis.net/downloads/microsoft/url-rewrite找到 URL Rewrite 扩展

【讨论】:

  • 此解决方案适用于托管在 IIS 10 上的 Angular 6 应用程序
  • 当我将它添加到我的 web,config 文件时,它会在浏览网站时导致 500.19 错误。
  • @Paul ,您是否在 IIS 6 上运行您的网站?
  • 感谢好友帮助重定向到 https。干杯:)
猜你喜欢
  • 1970-01-01
  • 2018-08-21
  • 2018-01-15
  • 2020-03-19
  • 2020-11-06
  • 2011-09-27
  • 2021-02-22
  • 2012-10-08
  • 1970-01-01
相关资源
最近更新 更多