64

I am trying to remove the # sign from the url in Angular 2 but I couldn't find any good explanation about how to remove it without generate any problem.

I remember on AngularJS 1 it was easier adding $locationProvider.html5Mode(true);

Also I would appreciate if you can tell me if this is a good practice (removing #) or could affect the SEO for the application (or improve it).

PS: I am using Angular 2 with typescript

3 Answers 3

98

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

7
  • 2
    Folks, In ng build --prod this solution is not working, gives 404 error..any clue?
    – HydTechie
    Commented Jul 26, 2017 at 13:55
  • 6
    This solves the problem of # removal but when we refresh the page or h5 this doen't work. Commented Nov 4, 2017 at 6:13
  • 2
    @HydTechie, JinnaBalu, When using PathLocationStrategy (HTML5 routing), you need to configure your webserver to serve index.html file (app's entry point) for all requested urls.
    – seidme
    Commented Dec 17, 2017 at 3:34
  • HI Seid, can you please let me know what and how to configure the web-server, when using the PathLocationStrategy.
    – user9630712
    Commented Jun 7, 2018 at 11:31
  • 1
    @SuchetaShrivastava The Angular docs added a section of examples for web server configurations for PathLocationStrategy implementations: angular.io/guide/deployment#fallback-configuration-examples
    – Talador12
    Commented Aug 10, 2018 at 19:40
41

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

6
  • 1
    its working for ng serve, but not for ng build --prod, any clue? thanks!
    – HydTechie
    Commented Jul 17, 2017 at 14:36
  • The error is 404, the link without # is showing as 404 in IIS.. I know there were some setting required to setup in iis to redirect, but i was looking without that, if possible. appreciate!
    – HydTechie
    Commented Jul 19, 2017 at 7:15
  • @HydTechie no its not. Look for SPApageFallback package Commented Jul 19, 2017 at 7:50
  • @VolodymyrBilyachat, If I use HashLocationStrategy , the navigate does not work in both developement and production any Idea ? Commented Nov 13, 2017 at 18:47
  • @VolodymyrBilyachat this tric is not working on production... but works in local Commented Dec 14, 2019 at 9:45
24

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>
1
  • 2
    can you give a brief explanation about "/app-context/". Commented May 6, 2019 at 8:25

Not the answer you're looking for? Browse other questions tagged or ask your own question.