代码之家  ›  专栏  ›  技术社区  ›  Mauricio Gonzalez

如何在Angular 5中从http重定向到https?

  •  2
  • Mauricio Gonzalez  · 技术社区  · 8 年前

    我刚刚在服务器上构建了一个Angular 5项目。当我写URL时,它工作得很好 https://www.example.com/ ,但当我写www.example时。com或 http://example.com 在浏览器中,它不会重定向。是否有一个文件,我可以在其中修改一些代码?我使用的是Windows server,它运行IIS。非常感谢。

    1 回复  |  直到 8 年前
        1
  •  2
  •   Stefan Falk    7 年前

    您需要在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,但是每次移动到不同的服务器时,都必须重新配置它):