1

Im creating an angular 2 app( previous angular 1 developer ) and im finding it difficult to get angular 2 urls to work with my server.

Basically, angular 2 doesnt use #, which means when an in-app route changes from "/" to pages/1, then angular loads pages/1 component and all data using ajax, and displays the pages. It works nice. However, when user refreshes the pages, then ...the page wont work.

Is there something i can do/configure, to make the angular app to reload the page at "/", but provide some hidden parameters to server like "pages/1" so that after page gets reloaded at "/", angular would know that user wanted to go to "pages/1" and do the redirect.

Rainer Plumer
  • 3,693
  • 2
  • 24
  • 42
  • You need to enable Hashlocation so that angular router understands # based URL's see this [How to bootstrap with HashLocationStrategy in Angular 2](http://stackoverflow.com/questions/38964450/how-to-bootstrap-with-hashlocationstrategy-in-angular-2-rc5/38964658#38964658), also you can read more about it [here](https://angular.io/docs/ts/latest/guide/router.html#!#browser-url-styles). Hope this helps!! – Madhu Ranjan Feb 21 '17 at 20:39
  • The answer from the duplicate question doesn't cover apache. – Altimus Prime Jan 13 '18 at 02:05

1 Answers1

3

You can switch to using hash:

imports: [
    /* ... */
    RouterModule.forRoot(appRoutes, { useHash: true })
]

To support no-hash urls, the server must be configured to respond with the same index.html file for any requested url.

admax
  • 1,671
  • 2
  • 16
  • 23
  • If you want your website to be indexed by google **don't use hash**! (https://www.seroundtable.com/google-no-hashtags-in-urls-26537.html) – Stefan Falk Dec 21 '18 at 08:11