6

Background: Angular 4, ng cli, RouterModule, useHash: true.

when I browse to my app using http://server/index.html it resolves to http://server/#/ (index.html is omitted from the url) additionally every routing navigation also omits the index.html from the url.

This is problematic for how my app is hosted, I need to keep index.html in the url. How can I configure angular to keep it?

12
  • why would this be a problem for your server? if no document is specifically requested, index.html should be the default for the server? Commented Jun 18, 2017 at 13:51
  • 1
    that's a very non-standard server; It doesn't seem like it will work with Angular (or any modern SPA framework). What server is this, exactly? Commented Jun 18, 2017 at 14:18
  • 1
    why are you trying to use Azure Storage as a web host? There are other Azure services designed specifically for this task..... Commented Jun 18, 2017 at 14:35
  • 1
    It's the most cost effective, and BTW it's a common pattern: learn.microsoft.com/en-us/azure/architecture/patterns/… , just happens to be the implementation on Azure Storage is lacking this feature. Not by chance this is #2 most voted issue. If I did the same on S3 I don't think it would raise any eyebrows Commented Jun 18, 2017 at 15:02
  • 1
    well honestly that's the answer. Angular is a dynamic framework, and you have listed documentation regarding usage of a storage service for hosting static content. This isn't an angular issue. period. Commented Jun 18, 2017 at 15:05

3 Answers 3

9

You can try:

ng build --prod --base-href /app/index.html

but it might affect relative path to your assets.

Sign up to request clarification or add additional context in comments.

2 Comments

Can also add it to angular-cli.json. This helped me on building my chrome extension using angular 2+. The extension got lost if I didn't had the index.html as a base path.
you can also set the base href value in the index.html file
0

index.html is not a known route by the Angular router. Once it is loaded into memory it redirects unknowns to the default route which is usually '\' and wipes out the index.html and any additional path or query parameters.

Something like this should do the trick ...

[
    { path: 'index.html', component: AppComponent},
    { path: '', redirectTo: 'index.html', pathMatch: 'full' },
    { path: '**', redirectTo: 'index.html', pathMatch: 'full' }
]

Also answered here: How to preserve index.html and query params in URL in Angular 6?

Comments

0

https://github.com/angular/angular/issues/36688#issuecomment-660278674

  1. "Remove the base href. Angular will not modify anything before the hash when using HashLocationStrategy if base href is not defined. "

  2. In addition during build step replaces all relative assets urls with absolute.

With this two steps angular Hash router will work even if you deploy your app in any subfolder of a static server (eg Azure Storage).

Eg

https://domain.azureedge.net/any/path/

Your app entry page can be any name, doesn't have to be index.html

https://domain.azureedge.net/any/path/any-name.html

Without base href:

https://domain.azureedge.net/any/path/any-name.html#/angular/route

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.