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

前端 未结 3 1392
挽巷
挽巷 2021-01-19 23:57

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?

相关标签:
3条回答
  • 2021-01-20 00:03

    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>
    
    0 讨论(0)
  • 2021-01-20 00:06

    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

    0 讨论(0)
  • 2021-01-20 00:20

    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>
    
    0 讨论(0)
提交回复
热议问题