11

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);
0

3 Answers 3

36

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="/">
8
  • 2
    It's already like this "RouterModule.forRoot(yourRoutesHere)". And if I remove the "{provide: LocationStrategy, useClass: HashLocationStrategy}" I will get the 404 error when I reload the page. Jan 15, 2017 at 15:02
  • Updated answer, try with PathLocationStrategy
    – AT82
    Jan 15, 2017 at 15:13
  • no lucky with the PathLocationStrategy, still getting the 404 error when reloading the page :( Jan 15, 2017 at 15:28
  • Okay, then it seems there could be something going on with your routes. Could you add your routing module to your question?
    – AT82
    Jan 15, 2017 at 15:29
  • Routing module added Jan 15, 2017 at 15:35
9

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.

2
  • is it intented to have both links to same page? Mar 13, 2019 at 15:27
  • i dont wanth to use useHash. Then how to i solve this issue Sep 24, 2019 at 11:21
4

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]
2
  • This is actually a very great answer. It helped me a lot. I did not use however the first rule. Is it necessary? Oct 10, 2020 at 20:09
  • it's depends upon your language you are using! Above code was for Angular 9
    – Sanchit
    Jan 3, 2021 at 18:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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