How to redirect from http to https in Angular 5?

I just built an Angular 5 project on the server. It works fine when I write the URL https://www.example.com/, but when I simply write www.example.com or http://example.com in the browser it won't redirect. Is there a file where I can modify this with some code? I'm using Windows server and it runs IIS. Thank you!

1 answer

  • answered 2018-01-10 21:35 Neyxo

    You need to configure this in your IIS configuration and not inside your Angular code, since it is a URL rewrite and not a duplication of the Website.

    Either you configure it with an web.config file:

    <?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>
    

    If you don't know how to use IIS configuration files i can recommend you to read the Microsoft documentation.

    Or you could use a Tool like described in this Microsoft Blog (Maybe a little bit easier, because it has a GUI, however you will have to reconfigure it each time you move to a different server):