azure 静态 webapp 将子域重定向到内部路径

问题描述 投票:0回答:2

我正在使用

Azure static web app
运行一个角度应用程序。
Is there a way to redirect subdomain to an internal path.

比方说:

abc.mydomain.com. to mydomain.com/abc

我已经在我的

CNAME
中配置了
DNS
。 我尝试过配置
.htaccess
文件。 (还有
web.config
。即使我知道服务器是ubuntu)。 他们都没有工作。 还尝试配置
staticwebapp.config.json
但它似乎适用于内部路由而不是子域。

提前致谢

angular azure http-redirect web-applications static
2个回答
0
投票

您可以直接在代码中添加重定向路由并编辑您的

staticwebapp.config.json
,如下所示:-

app-routing.module.ts

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { HomeComponent } from './home/home.component'; 
const routes: Routes = [
  { path: '', component: HomeComponent },
  { path: 'abc', component: AbcComponent },

@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule]
})
export class AppRoutingModule { }

staticwebapp.config.json

{
  "routes": [
    {
      "route": "/abc/*",
      "rewrite": "/index.html"
    }
  ]
}

如果您没有路线,请将代码直接添加到您的

app.component.ts
:-

import { Component } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';

@Component({
  selector: 'app-root',
  template: `
    <div>
      <router-outlet></router-outlet>
    </div>
  `,
})
export class AppComponent {
  constructor(private route: ActivatedRoute, private router: Router) {
    this.route.url.subscribe(url => {
      const path = url[0].path;
      if (path === 'abc') {
        window.location.href = 'https://customdomain.com/abc';
      }
    });
  }
}

enter image description here


0
投票

似乎唯一的解决方案是在角度内进行。在 app.component.ts 中: ngOnInit(): 无效 {

var link = window.location.hostname
var subdomain = link.split(".")[0]


if(subdomain == link.split(".")[1]){/*do nothing*/}
else if(subdomain != ''){
  this.router.navigate([subdomain]);
}

}

}

© www.soinside.com 2019 - 2024. All rights reserved.