简体   繁体   English

Angular 2+ 将所有非尾随反斜杠 URL 重定向到尾随反斜杠 URL

[英]Angular 2+ Redirect all non trailing backlash urls to trailing backslash urls

In my angular app i have large number of routes and i want a 301 redirect of all non trailing backslash to trailing backslash urls handled from server.ts在我的 angular 应用程序中,我有大量路由,我希望将所有非尾随反斜杠 301 重定向到从 server.ts 处理的尾随反斜杠 URL

what I want is like this我想要的是这样的

app.get('/about', function (req, res, next) {  
  return res.redirect('/about/'); 
});

app.get('/person/john', function (req, res, next) {  
  return res.redirect('/person/john/'); 
});

how can i achieve for all urls, how is it possible?我怎样才能实现所有网址,这怎么可能? including query parameters包括查询参数

You can write routes like this:你可以这样写路由:

export const fooRoutes: Routes = [
    {
        path: '/about', component: AboutComponent
    }
];

It is not recommended to do it in Angular.在 Angular 中不建议这样做。

The only logical way to do this is on your server settings like IIS, nginx, etc.唯一合乎逻辑的方法是在您的服务器设置上,例如 IIS、nginx 等。

There are some wired ways in the internet that can be done in Angular.网上有一些有线方式可以在Angular中完成。 but I don't recommend them at all.但我根本不推荐它们。 The only logical way is by server side.唯一合乎逻辑的方式是通过服务器端。

One one them can be found in here:一个可以在这里找到:

https://github.com/angular/angular/issues/14905#issuecomment-283995900 https://github.com/angular/angular/issues/14905#issuecomment-283995900

This one actually removes it.这个实际上删除了它。 but by understanding the idea, you can Add trailing slash.但通过理解这个想法,您可以添加斜杠。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM