3

So I defined some routes /home, /contact, ... Created the dist folder by using ng build --prod --base-href and copied the content of the dist folder via filezilla to the hosting server. When directly browsing the website and redirecting from on the website there is no problem but if you directly browse to website/home, it says 404 Not Found The requested URL /home was not found on this server.

The hosting support responded with these are probably CleanURL's defined in a .htaccess file we can't give support for that.

I thought that clean urls were readable restcalls but maybe the meaning is greater than only rest. But is there a solution for this?

Burst of Ice
  • 386
  • 2
  • 6
  • 23

1 Answers1

1

more likely you have not defined the HashLocationStrategy in your providers (in app.module.ts)

Updated here.. i tried to put into the comments but it can't format

There are 3 ways 1st way

@NgModule({
  imports: [
    BrowserModule,
    FormsModule,..
    RouterModule.forRoot(routes, { useHash: true }) 
  ],
  declarations: [
    AppComponent, ...
  ],
  providers: [],
  bootstrap: [ AppComponent ]
})

2nd way

@NgModule({
  declarations: [
    AppComponent, ...
  ],
  imports: [
    AppRoutingModule,
    BrowserModule,
    BrowserAnimationsModule,
    FormsModule,
    HttpModule, ...
  ],
  providers: [
    { provide: LocationStrategy, useClass: HashLocationStrategy }
  ],
  entryComponents: [],
  bootstrap: [AppComponent]
}) 

3rd way - recommended for Angular2 Use PathLocationStrategy

import {APP_BASE_HREF} from '@angular/common';

@NgModule({
providers: [{provide: APP_BASE_HREF, useValue: '/my/app'}]
})

There are pros and cons - i used 2nd as I came from angular1, 2,4,5 (old school) :p

erickyi2006
  • 216
  • 2
  • 8
  • So I have to do `RouterModule.forRoot(routes, {useHash: true})` instead of just giving in the routes then? – Burst of Ice Apr 04 '18 at 07:39
  • yes, see updated text above (I tried to comment here but i can't format). – erickyi2006 Apr 04 '18 at 13:52
  • method 1 and 2 give: http://localhost:4200/#/home a /#/ between my existing routes, 3rd way works locally I'll try on the apache server, do I still need a .htaccess file which redirects direct browsing to clean urls to index.html? – Burst of Ice Apr 04 '18 at 17:40
  • in theory, no. because if your current settings go to your app, the routing will handle the rest for you. p/s i didn't use apache. i m using nodejs express to serve our REST requests – erickyi2006 Apr 05 '18 at 00:18
  • my '' route redirects to '/home' so that's probably why it happend but It works so thanks. – Burst of Ice Apr 09 '18 at 12:32