IIS上的主机Angular应用程序-重定向到root并强制HTTPS

IIS上的主机Angular应用程序-重定向到root并强制HTTPS,angular,redirect,iis,https,url-rewrite-module,Angular,Redirect,Iis,Https,Url Rewrite Module,我在IIS中托管了一个angular应用程序。为了将所有请求重定向到应用程序的根目录,并允许角度路由处理不同的路由,我添加了一个带有URL重写规则的web.config文件,如下所示: <configuration> <system.webServer> <rewrite> <rules> <rule name="Angular Routes" stopProcessing="true"> <

我在IIS中托管了一个angular应用程序。为了将所有请求重定向到应用程序的根目录,并允许角度路由处理不同的路由,我添加了一个带有URL重写规则的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>

强制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/core'导入{Component,OnInit};
从“@angular/common”导入{Location};
从“../environments/environment”导入{environment};
@组成部分({
选择器:“vp应用程序根”,
templateUrl:“./app.component.html”,
样式URL:['./app.component.scss']
})
导出类AppComponent实现OnInit{
标题=‘vp应用程序’;
地点:地点;
恩戈尼尼特(){
if(环境、生产){
如果(location.protocol==='http:'){
window.location.href=location.href.replace('http','https');
}
}
}
}

重定向到HTTPS&重定向到ROOT

如果某人仍在寻找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>


我也收到同样的问题。。你能解决它吗?我在Angular项目中直接强制使用https,只需要几行代码。如果你有兴趣做同样的事情,让我知道,我会与你分享我的代码。。这将对我很有帮助。我的项目在angular 5.3.2中,API项目在dot net core 2.1.3中。谢谢。当然,我发布了备选解决方案作为答案。该解决方案适用于IIS 10上托管的Angular 6应用程序。当我将其添加到我的web配置文件时,它会在浏览网站时导致500.19错误。@Paul,你是在IIS 6上运行网站吗?谢谢,它帮助重定向到https。干杯:)这会发生在重定向到身份提供者之前吗?只在第一个链接上对我有效,在所有其他链接上,它是HTTP