Full answer
A single-page application (SPA) downloads one HTML file and one set of scripts. When the user clicks a link, the Angular router does not ask the server for a new page. It changes the URL with the browser's History API and shows other components.
Path strategy (default, PathLocationStrategy). URLs look like /courses/42. Because the browser can reload at that URL, the server must be configured to return index.html for any path that is not a real file. If it is not, a reload or a shared link gives a 404 from the server. Angular dev server and most hosting platforms offer a "rewrite to index.html" setting. The <base href="/"> tag in index.html tells the router where the app lives, and must be correct when the app is hosted in a sub folder.
Hash strategy (HashLocationStrategy). URLs look like /#/courses/42. The part after # is never sent to the server, so any static host works without rewrite rules. You enable it with provideRouter(routes, withHashLocation()). The trade-offs: uglier URLs, hash fragments are no longer free for in-page anchors, and search engines handle them poorly.
Which to choose: path strategy for public sites where SEO and clean links matter, with server rewrites. Hash strategy for internal tools or when you cannot control the server.
Related tools: the Location service reads the path and calls back() and forward(), and APP_BASE_HREF sets the base in code. With server-side rendering the server itself handles every route, so the fallback rule is not needed for rendered pages.
import { Component, inject } from '@angular/core';
import { Location } from '@angular/common';
import { ApplicationConfig } from '@angular/core';
import { Routes, provideRouter, withHashLocation } from '@angular/router';
@Component({
selector: 'app-home',
template: `
<p>Current path: {{ location.path() }}</p>
<button (click)="location.back()">Back</button>
`,
})
export class HomeComponent {
protected readonly location = inject(Location);
}
const routes: Routes = [{ path: 'courses/:id', component: HomeComponent }];
// Path strategy (default): address bar shows /courses/42, server must rewrite to index.html.
export const pathConfig: ApplicationConfig = {
providers: [provideRouter(routes)],
};
// Hash strategy: address bar shows /#/courses/42, works on any static host.
export const hashConfig: ApplicationConfig = {
providers: [provideRouter(routes, withHashLocation())],
};Angular 20.3, type-checked. The URLs in the comments show what the browser address bar would look like in each mode.
Key points
- An SPA changes components and the URL without full page loads, using the History API.
- Path strategy needs a server rewrite to `index.html`. Hash strategy does not.
- Enable hash mode with `withHashLocation()`. Set the base with `<base href>`.
- Choose path for public, SEO-sensitive sites.
Common mistakes
- Deploying a path-strategy app to static hosting without a rewrite rule, so refreshing a deep link returns 404.
- A wrong `<base href>` when hosting in a sub folder, so scripts and routes fail to load.
- Using the hash strategy and then expecting `#section` anchors to work as before.
Follow-ups
- How do you host an Angular app in a sub folder?
- Why does SSR remove the need for the index.html fallback on rendered routes?
- How does the router know the URL changed when the user presses the back button?
Tips
- Describe what the server sees in each mode. It makes the difference concrete.
- Mention the 404-on-refresh symptom, which every Angular developer meets once.
- Know where the base href lives and why.
Evaluation rubric
- Explains SPA navigation without page loads.
- Compares both strategies with server impact.
- Names the config to switch.