Warm tip: This article is reproduced from serverfault.com, please click

Angular

发布于 2020-12-01 18:45:17

I'm using angular and hashlocationstrategy:

my default link to site is https://mywebsite.com/#/

if i want to share on LinkedIn a sub route from my website like https://mywebsite.com/#/123 the link show as https://mywebsite.com/ #/123

enter image description here

I tried to remove # but angular use this strategies to load the correct page any idea to replace # or remove it and keep redirecting to the same point in angular ?

Questioner
Tarek Adra
Viewed
0
Krenom 2020-12-02 03:36:55

As Aakash commented, your choices are to:

  1. Use hash location strategy and let Angular handle the routing
  2. Don't use hash location strategy and set up your server to always serve index.html (or at least fall back to serving that if whatever x.html isn't found)

This is because browsers/servers ignore anything after #'s. That means that linking to http://example.com/#/something will load example.com's index.html page (or whatever the server is set up to serve as default). Once index is served, Angular loads, reads the route NOT ignoring everything after the # and proceeds to handle the routing.

Without #'s, going to the same http://example.com/something will attempt to load something.html, which for an angular app doesn't exist so the server will 404. The server needs to be set up to serve the base index.html in such events. Once said index is served, Angular loads, reads the route and proceeds to handle the routing.