BSSchwarzkopf
BSSchwarzkopf

Reputation: 458

Angular - Redirect based on prefix but drop remaining, non-matched path

I need to support legacy paths in my application but I want to redirect the user to the new, correct path. So for example, I have the following legacy paths:

/legacyRoute/subRoute, /legacyRoute/anotherSubRoute

and I want both of those to get redirected to:

/newRoute

I found the pathMatch option prefix in the Angular docs but setting my route to:

{path: 'legacyRoute', redirectTo: 'newRoute', pathMatch: 'prefix' }

results in a path that looks like /newRoute/subRoute

It's not a huge problem, the page still loads like I expect but it's bothering me nonetheless. Is there an Angular way to redirect based on a prefix and drop anything that doesn't match? Also, it would be nice if I can keep my query string parameters.

Upvotes: 1

Views: 736

Answers (1)

BSSchwarzkopf
BSSchwarzkopf

Reputation: 458

Dangit, found a solution right after posting. You can specify a subroute and not do anything with it like this:

{path: 'legacyRoute/:subRoute', redirectTo: 'newRoute', pathMatch: 'prefix' }

and that will result in: legacyRoute/whatever?blah=blah -> newRoute?blah=blah

Upvotes: 4

Related Questions