Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular2 without hash in the url

Now, my website's url looks like this because I'm using the approach described here

http://localhost:4200/#/cadastro

Is it possible to remove the hash in the url and not get the 404 error?

EDIT: Router Module added

const appRoutes: Routes = [
    { path: '', component: HomeComponent },
    { path: 'cadastro', component: CadastroNoivosComponent },
    { path: '**', component: HomeComponent }
];

export const routing = RouterModule.forRoot(appRoutes);
like image 428
Emilio Weba Avatar asked Jan 15 '17 14:01

Emilio Weba


People also ask

What is hash in URL angular?

There are two ways to support routing in single-page apps: Hashed URL Paths — We break the URL path with a # (Hash) so that the browser understands it's just a virtual fragment. Ordinary URL Paths (Non-Hashed URL Paths) — The server needs to intercept the request and return the index.

What is the difference between HashLocationStrategy and PathLocationStrategy?

Angular supports two different location strategies or Routing strategy in Angular. One is PathlocationStrategy and the other one is HashLocationStrategy . The HashLocationStrategy use the Hash style routing, while PathlocationStrategy uses the HTML 5 Routing.

How do you use PathLocationStrategy?

If you're using PathLocationStrategy , you may provide a APP_BASE_HREF or add a <base href> element to the document to override the default. For instance, if you provide an APP_BASE_HREF of '/my/app/' and call location.go('/foo') , the browser's URL will become example.com/my/app/foo .


3 Answers

If you are using Angular final, the reasons to the hash could be:

RouterModule.forRoot(yourRoutesHere, { useHash: true })

So by removing that could help.

RouterModule.forRoot(yourRoutesHere)

Alternatively if you in your providers (in NgModule) have used:

{provide: LocationStrategy, useClass: HashLocationStrategy}

just remove that.

EDIT, if you need LocationStrategy, try changing HashLocationStrategy to PathLocationStrategy:

{provide: LocationStrategy, useClass: PathLocationStrategy}

More about LocationStrategy here

Now that I have seen your routes as well regarding your 404 issue, you could try changing the following

{ path: '**', component: HomeComponent }

to:

{ path: '**', redirectTo: '', pathMatch: 'full' }

More about routing here

Also check that in your index.html you have set the basehref like so:

<base href="/">
like image 150
AT82 Avatar answered Oct 26 '22 08:10

AT82


If you use PathLocationStrategy as describe here you can remove the hash in the URL.

But getting rid of 404 error needs some server side tweak. A quick and easy way is to configure your server to load the home page when any URL of the form http://yourhost/* is requested.

like image 9
duke636 Avatar answered Oct 26 '22 08:10

duke636


Create a .htaccess file Paste the following Code And Upload on your prod Server.

RewriteEngine On
RewriteBase /
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.html [L]
like image 3
Sanchit Avatar answered Oct 26 '22 07:10

Sanchit