D.B*_*D.B 11 iis https redirect url-rewrite-module angular
我在IIS中托管了一个角度应用程序.为了将所有请求重定向到应用程序的根目录并允许角度路由处理不同的路由,我添加了一个带有URL Rewrite规则的web.config文件,如下所示:
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Angular Routes" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="/" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
Run Code Online (Sandbox Code Playgroud)
它工作正常.但是,我现在需要强制应用程序从HTTP重定向到HTTPS.为了做到这一点,我可以添加一个新规则,如下面的帖子描述的:Angular 2 - 总是重定向到https://而不是使用http://
规则强制HTTPS:
<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>
Run Code Online (Sandbox Code Playgroud)
此规则也适用,但它将我的重定向分解为先前定义的根规则.那么有没有人有一个想法我如何将这两个规则混合在一起或以其他方式实现这两个目的?
D.B*_*D.B 11
作为替代解决方案,我最终直接在角度项目中的主控制器上强制执行https,如下所示.(张贴在案例中对其他人有用)
import { Component, OnInit } from '@angular/core';
import { Location } from '@angular/common';
import { environment } from '../environments/environment';
@Component({
selector: 'vp-app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss']
})
export class AppComponent implements OnInit {
title = 'vp-app';
location: Location;
ngOnInit() {
if (environment.production) {
if (location.protocol === 'http:') {
window.location.href = location.href.replace('http', 'https');
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
重定向到HTTPS和重定向到根
仍在寻找web.config解决方案的人,将您现有的web.config文件代码替换为以下代码:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<!--START REDIRECT TO HTTPS-->
<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>
<!--END REDIRECT TO HTTPS-->
<!--START REDIRECT TO ROOT-->
<rule name="AngularJS" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="/" />
</rule>
<!--END REDIRECT TO ROOT-->
</rules>
</rewrite>
</system.webServer>
</configuration>
Run Code Online (Sandbox Code Playgroud)
注意: 对于IIS 6,您可能需要安装URL Rewrite扩展名才能使其起作用。
您可以在以下位置找到URL重写扩展名:https : //www.iis.net/downloads/microsoft/url-rewrite
| 归档时间: |
|
| 查看次数: |
6551 次 |
| 最近记录: |