Ben Cameron
Ben Cameron

Reputation: 4413

Angular 2 - always redirect to https:// instead of using http://

I have an Angular 2 app that I only want to run on https. What is the best way to redirect addresses that use http to use https instead?

Upvotes: 6

Views: 9042

Answers (3)

Tom Stickel
Tom Stickel

Reputation: 20401

I have to write in an exception rule for the web api service based on where the web api was located within same area :/

Notice this line

  <add input="{REQUEST_URI}" pattern="^/(C2NGService)" negate="true" />

web.config for angular 5

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

Upvotes: 0

Ben Cameron
Ben Cameron

Reputation: 4413

I have added a web.config to the Angular2 site which contains the below. Remember that this is for an IIS hosted application. All addresses are now redirected the https version.

<?xml version="1.0" encoding="UTF-8"?>
<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>

Upvotes: 5

Piyush Patil
Piyush Patil

Reputation: 14523

As per your question you will have to force https for you domain from the server side. As you mentioned IIS in your comment here are some of the ways that you can achieve force HTTps.

https://www.sslshopper.com/iis7-redirect-http-to-https.html

Upvotes: 0

Related Questions