Keywords: AngularJS | Routing Modes | URL Rewriting
Abstract: This article provides an in-depth exploration of three routing modes in AngularJS: Hashbang mode, HTML5 mode, and Hashbang in HTML5 mode. By analyzing the working principles of the $location service, it explains in detail how to properly configure URL rewriting in different modes, including settings for $locationProvider, decoration of the $sniffer service, and usage of the <base> tag. With concrete code examples, the article helps developers understand how AngularJS uniformly handles client-side routing, avoiding the tedious work of manual URL rewriting.
In AngularJS application development, routing management is a core aspect of building single-page applications (SPAs). The $location service provides two main URL modes: HTML5 mode and Hashbang mode. However, many developers have misconceptions about URL rewriting mechanisms in these modes, particularly regarding link handling in templates. Based on AngularJS official documentation and community best practices, this article delves into three routing modes and their configuration methods.
Three Routing Modes in AngularJS
AngularJS's routing system actually supports three distinct modes, each with different URL handling strategies. Understanding the differences between these modes is crucial for proper application configuration.
Hashbang Mode
Hashbang mode is the default routing mode in AngularJS, using hash fragments (#) in URLs to manage routing states. The advantage of this mode is excellent compatibility, working in all browsers without server-side configuration. Configuration example:
$locationProvider
.html5Mode(false)
.hashPrefix('!');
$routeProvider.when('/path', {
templateUrl: 'path.html'
});
In this mode, all links in templates must include the complete hash prefix. For example:
<a href="index.html#!/path">Link</a>
The browser displays URLs in the format: http://www.example.com/base/index.html#!/base/path. While this mode offers good compatibility, URLs are less aesthetic and not SEO-friendly.
HTML5 Mode
HTML5 mode utilizes the History API in modern browsers to achieve clean URLs without hashes. This mode provides better user experience and SEO benefits but requires server-side URL rewriting. Configuration method:
$locationProvider.html5Mode(true);
$routeProvider.when('/path', {
templateUrl: 'path.html'
});
Additionally, the <base> tag must be set in the <head> section of the HTML file:
<html>
<head>
<base href="/">
</head>
</html>
In HTML5 mode, links in templates can use standard paths directly:
<a href="/path">Link</a>
Browser URLs appear as: http://www.example.com/base/path. Note that the server must be configured to redirect all route requests to the application's entry file; otherwise, direct access to deep links will result in 404 errors.
Hashbang in HTML5 Mode
This is a special hybrid mode that AngularJS automatically falls back to when the application is configured for HTML5 mode but runs in a browser without History API support. Developers can also simulate this environment by decorating the $sniffer service:
$provide.decorator('$sniffer', function($delegate) {
$delegate.history = false;
return $delegate;
});
$locationProvider
.html5Mode(true)
.hashPrefix('!');
$routeProvider.when('/path', {
templateUrl: 'path.html'
});
The <base> tag is also required, and links in templates can maintain the HTML5 mode style:
<a href="/path">Link</a>
However, in browsers without History API support, the actual URL becomes: http://www.example.com/index.html#!/base/path. This mode ensures application compatibility in older browsers while providing better URL experience in modern browsers.
Core Mechanism of URL Rewriting
AngularJS automatically handles link rewriting in templates through the $location service. The key lies in correctly configuring $locationProvider and the <base> tag. When html5Mode is set to true, AngularJS attempts to use the History API; when set to false, it uses hash routing. The $sniffer service detects browser support for the History API, and developers can force specific behavior patterns by decorating this service.
Practical Application Recommendations
For new projects, it is recommended to prioritize HTML5 mode as it provides more user-friendly URLs and better SEO results. Simultaneously, ensure server-side route rewriting is configured so that direct access to any route correctly returns the application entry file. For projects requiring support for older browsers, leverage Hashbang in HTML5 mode for graceful degradation. Regardless of the chosen mode, use relative paths in templates instead of hardcoded URL prefixes, allowing AngularJS to automatically handle mode switching.
By properly understanding how these three routing modes work, developers can avoid the tedious task of manual URL rewriting, letting AngularJS automatically handle routing compatibility issues across different environments. This not only improves development efficiency but also ensures consistent application behavior across various browsers and devices.