Home > database >  How to catch unhashed paths using hashed location strategy?
How to catch unhashed paths using hashed location strategy?

Time:05-26

I've had troubles solving this. We're using HashLocationStrategy in our application and for some cases we will not be changing this. But I have to catch certain paths containing a series of numbers like this:

www.someurl.com/1241234

I have implemented a Angular directive to catch these by subscribing router events. It succeeds to catch when user types url with hashed location like this:

www.someurl.com/#/1241234

the subscription in directive catches 1241234 number correctly and I am able to apply my logic before redirecting to 404 not found page as intended (successfully). But I have to also catch the series when user types the url as www.someurl.com/1241234 but this time application automatically redirects www.someurl.com/#/notfoundpage.

Is there a way to redirect www.someurl.com/1241234 to www.someurl.com/#/1241234 or handle path location strategy www.someurl.com/1241234 somehow?

edit: number can change. So it may be like www.someurl.com/6345, www.someurl.com/124, www.someurl.com/983 etc.

CodePudding user response:

I think your best bet is to check the location and redirect before you bootstrap your angular application:

... imports

//check if path is a number /1234
if (location.pathname.match(/^\/\d $/)) {
  //add a hash in front of the pathname
  location.replace('/#'   location.pathname);
} 

//bootstrap angular
platformBrowserDynamic()
  .bootstrapModule(AppModule)
  .catch((err) => console.error(err));

  • Related