您需要在IIS(Internet Information Service)配置中配置此项,而不是在Angular代码中,因为它是URL重写,而不是网站的副本。
您可以使用web对其进行配置。配置文件:
<?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>
如果您不知道如何使用IIS配置文件,我建议您阅读
Microsoft documentation
.
或者可以使用中描述的工具
this Microsoft Blog
(可能更容易一些,因为它有一个GUI,但是每次移动到不同的服务器时,都必须重新配置它):