Search code examples
angularangular7urlencodeurldecode

Encoded url is not redirecting properly in angular, how to route encoded url to proper address?


how to redirect encoded special characters url to proper address? in my project encoded url routing to web page. i created route something like this

{ path: 'welcome/:id', component: WelcomeComponent },

actually i am passing url like http://localhost/welcome%3Fid%3D45 but this is not accepting and it is accepting only http://localhost/welcome?id=45


Solution

  • {path: '**', component: 'NotFoundComponent'}
    
    export class NotFoundComponent{
    constructor(private router:Router,
              private route: ActivatedRoute,
              ) { 
                let url = this.router.url;
                url = url.replace(/%40/gi, '@')
                .replace(/%3A/gi, ':')
                .replace(/%24/gi, '$')
                .replace(/%2C/gi, ',')
                .replace(/%3B/gi, ';')
                .replace(/%2B/gi, '+')
                .replace(/%3D/gi, '=')
                .replace(/%3F/gi, '?')
                .replace(/%2F/gi, '/');
                if(url !== this.router.url){
                     this.router.navigate([url]);
                  }
    }