Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

angular

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?

like image 640
Ben Cameron Avatar asked Jul 12 '16 16:07

Ben Cameron


People also ask

Why does HTTP change automatically to HTTPS?

HSTS is a security feature that forces the browser to use HTTPS even when accessing an HTTP URL. The browser will start using HSTS for a domain after receiving a Strict-Transport-Security header from the server. The browser also ships with a list of domains for which HSTS is enabled by default.

Should you redirect HTTP to HTTPS?

Migrating your site from HTTP to HTTPS can seem like an overwhelming and complex process. However, with big security benefits and SEO advantages, it makes sense to migrate from HTTP to HTTPS. The process needn't be difficult either. The key is to migrate using 301 to redirect HTTP to HTTPS.


1 Answers

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>
like image 193
Ben Cameron Avatar answered Nov 15 '22 09:11

Ben Cameron