我正在使用
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
但它似乎适用于内部路由而不是子域。
提前致谢
您可以直接在代码中添加重定向路由并编辑您的
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';
}
});
}
}
似乎唯一的解决方案是在角度内进行。在 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]);
}
}
}