简体   繁体   English

Angular 2 - 始终重定向到https://而不是使用http://

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

I have an Angular 2 app that I only want to run on https. 我有一个Angular 2应用程序,我只想在https上运行。 What is the best way to redirect addresses that use http to use https instead? 重定向使用http的地址改为使用https的最佳方法是什么?

I have added a web.config to the Angular2 site which contains the below. 我已将web.config添加到Angular2站点,其中包含以下内容。 Remember that this is for an IIS hosted application. 请记住,这适用于IIS托管的应用程序。 All addresses are now redirected the https version. 现在所有地址都重定向到https版本。

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

As per your question you will have to force https for you domain from the server side. 根据您的问题,您必须从服务器端强制为您的域https。 As you mentioned IIS in your comment here are some of the ways that you can achieve force HTTps. 正如您在评论中提到的IIS,这里有一些方法可以实现强制HTTps。

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

I have to write in an exception rule for the web api service based on where the web api was located within same area :/ 我必须根据web api在同一区域内的位置编写web api服务的例外规则:/

Notice this line 注意这一行

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

web.config for angular 5 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM