Angular 2 Remove Hash (#) from the URL

In angular there is location strategy

Look into app.module.ts where app is bootstrapped there you have

@NgModule({
.......
  providers: [
....
    { provide: LocationStrategy, useClass: HashLocationStrategy },
....
]
});

And remove this part since PathLocationStrategy is default strategy


Above answers have the correct explanation about removing the Hash from the URL, but when you change LocationStrategy your back-end will be affected because the back-end doesn't understand all of your Angular 2 Routes. Here are the steps to remove hash with the back-end support.

1) Change Angular to use PathLocationStrategy

@NgModule({
  .....
  providers: [
    // Below line is optional as default LocationStrategy is PathLocationStrategy
    {provide: LocationStrategy, useClass: PathLocationStrategy} 
  ]
})

2) Change the base Href in index.html, Angular2 will handle all routes post base Href

<base href="/app-context/">

For example

<base href="/app/">

3) At the backend server, we must render the index.html file for any request coming with below pattern

"/app/**" - Render index.html for any request coming with "/app/**" pattern

index.html

<!doctype html>
<html>
  <head>
    <meta charset="utf-8">
    <title>My App</title>
    <base href="/app/">
  </head>
  <body>
    <app-root>Loading...</app-root>
    <script type="text/javascript" src="vendor.bundle.js"></script>
    <script type="text/javascript" src="main.bundle.js"></script>
  </body>
</html>

As @Volodymyr Bilyachat pointed out, PathLocationStrategy is a default location strategy in Angular2, and if the # is present in the url, it must have been that's overridden somewhere.

Beside the module providers, check your module imports, it can also be overridden by providing the { useHash: true } as the second argument of the RouterModule.forRoot:

imports: [
    ...
    RouterModule.forRoot(routes, { useHash: true })  // remove second argument
]

Also note that when using PathLocationStrategy you need to configure your web server to serve index.html (app's entry point) for all requested locations.

Here are configuration examples for some of the popular web servers: https://angular.io/guide/deployment#fallback-configuration-examples