【问题标题】:Angular 2 - always redirect to https:// instead of using http://Angular 2 - 总是重定向到 https:// 而不是使用 http://
【发布时间】:2016-11-15 01:12:47
【问题描述】:

我有一个 Angular 2 应用程序,我只想在 https 上运行。 将使用 http 的地址重定向到使用 https 的最佳方法是什么?

【问题讨论】:

  • 您的应用托管在哪里?
  • 它在 Azure 中作为 Web 应用程序。
  • 你使用的是 Apache Linux 还是其他的东西?
  • IIS 我相信是 Azure 中的默认设置。

标签: angular


【解决方案1】:

根据您的问题,您必须从服务器端为您的域强制使用 https。正如您在评论中提到的 IIS 是您可以实现强制 HTTP 的一些方法。

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

【讨论】:

    【解决方案2】:

    我已将 web.config 添加到 Angular2 站点,其中包含以下内容。请记住,这是针对 IIS 托管应用程序的。 所有地址现在都重定向到 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>
    

    【讨论】:

    • 你把这个文件放在哪里?
    • 使用 iis 时位于网站的根目录中。
    • 好的。它不起作用,因为我的服务器上没有安装 URL 重写。在结合此规则安装它之后,它正在工作。谢谢。
    • 我有类似的情况,但对我来说,应用程序是通过 Angular -cli 创建的。所以我不能明确地这个文件。 Azure 是我的服务器。有什么解决办法吗?
    • 您能否在构建过程中复制该文件?
    【解决方案3】:

    我必须根据 web api 在同一区域内的位置为 web api 服务编写例外规则:/

    注意这一行

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

    角度 5 的 web.config

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-12-12
      • 2018-01-02
      • 2014-10-07
      • 2019-06-24
      • 1970-01-01
      • 2020-06-18
      • 2019-08-04
      相关资源
      最近更新 更多