Search code examples
angulartypescriptangular-universalangular4-forms

Angular 4 + Universal Redirect to 404 page if found any invalid Url


I am using Angular 4 + universal for my application and using below code to redirect to 404 page if some unknown URL is found.

  {
path: '',
component: FullLayoutComponent,
data: {
  title: 'Home'
},
children: [
  {
    path: '',
    loadChildren: 'app/core/components/static/static.module#StaticModule'
  },
  {
    path: 'move',
    loadChildren: 'app/core/components/move/move.module#MoveModule'
  },
  {
    path: 'account',
    loadChildren: 'app/core/components/account/account.module#AccountModule'
  },
  { path: "**",redirectTo:"404"}
]

}

it redirects to localhost:4200/404 for any unknown URL but if I go to URL localhost:4200 then also it redirects me to 404 .

any leads will surely help a lot.

Thanks


Solution

  • change your code to as follows -:

      {
    path: '',
    component: FullLayoutComponent,
    data: {
     title: 'Home'
    },
    children: [
      {
       path: '',
       loadChildren:'app/core/components/static/static.module#StaticModule'
    },
    {
     path: 'move',
     loadChildren: 'app/core/components/move/move.module#MoveModule'
    },
    {
     path: 'account',
     loadChildren: 'app/core/components/account/account.module#AccountModule'
    }
    
     ]
    },
    
    { path: "**",redirectTo:"404"}