Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Angular 6 how make case insensitive url pattern?

In my case I want to support same url in case insensitive manner.

Example: it should support all url

localhost:1029/documentation
localhost:1029/DOCUMENTATION
localhost:1029/DOCUMENTAtion
localhost:1029/docuMENTATION
like image 296
Jitendra Avatar asked Aug 23 '18 12:08

Jitendra


People also ask

Are Angular routes case sensitive?

The routes in Angular are case sensitive. In the routeconfig, you have specified the url path as "product".

Are URL paths case sensitive?

It may surprise you but, yes, URLs are case sensitive. And, if you have both upper- and lowercase versions of your site's domain, you may be unintentionally making Google's job harder — and hurting your site's own performance.

How do I make a route sensitive case in Angularjs?

That's because the routes that are configured using UI-Router are case sensitive. To make route insensitive, all you have to do is to inject $urlMatcherFactoryProvider service into the config() function and call caseInsensitive(true) function. Let's go back to our Controller.

What is path match in Angular?

pathMatch = 'prefix' tells the router to match the redirect route when the remaining URL begins with the redirect route's prefix path. Ref: https://angular.io/guide/router#set-up-redirects. pathMatch: 'full' means, that the whole URL path needs to match and is consumed by the route matching algorithm.


2 Answers

You should add this provide statement to the app.module.ts

    import { DefaultUrlSerializer, UrlTree } from '@angular/router';
    
    export class LowerCaseUrlSerializer extends DefaultUrlSerializer {
        parse(url: string): UrlTree {
            // Optional Step: Do some stuff with the url if needed.
    
            // If you lower it in the optional step 
            // you don't need to use "toLowerCase" 
            // when you pass it down to the next function
            return super.parse(url.toLowerCase()); 
        }
    }

And

    @NgModule({
        imports: [
          ...
        ],
        declarations: [AppComponent],
        providers: [
            {
                provide: UrlSerializer,
                useClass: LowerCaseUrlSerializer
            }
        ],
        bootstrap: [AppComponent]
    })
like image 143
mittal bhatt Avatar answered Sep 18 '22 21:09

mittal bhatt


You need a UrlSerializer as follow:

import { DefaultUrlSerializer, UrlTree } from '@angular/router';


 export class LowerCaseUrlSerializer extends DefaultUrlSerializer {
   parse(url: string): UrlTree {
      return super.parse(url.toLowerCase());
  }
}

And then added it as a provider in the app.module.ts

providers: [
 {
   provide: UrlSerializer,
   useClass: LowerCaseUrlSerializer
}
]
like image 33
foram kantaria Avatar answered Sep 18 '22 21:09

foram kantaria