Host Angular app on IIS - Redirect to root and force HTTPS
As an alternative solution I ended up forcing https on the main controller in the angular project directly, as follows. (Posted in case is useful for someone else)
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');
}
}
}
}
REDIRECT TO HTTPS & REDIRECT TO ROOT
Someone who is still looking for web.config solution, replace your existing web.config file code with the following code:
<?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>
Note: For IIS 6 you may need to install URL Rewrite extension to make it work.
You can find URL Rewrite extension at: https://www.iis.net/downloads/microsoft/url-rewrite