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
In angular there is location strategy
Look into app.module.ts where app is bootstrapped there you have
And remove this part since PathLocationStrategy is default strategy
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 theRouterModule.forRoot
:Also note that when using
PathLocationStrategy
you need to configure your web server to serveindex.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
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 yourAngular 2
Routes. Here are the steps to remove hash with the back-end support.1) Change Angular to use PathLocationStrategy
2) Change the base Href in index.html, Angular2 will handle all routes post base Href
For example
3) At the backend server, we must render the index.html file for any request coming with below pattern
index.html