Javascript

AngularJS routing without the hash

25 September 2026 · 9 min read

AngularJS routing without the hash

AngularJS, a powerful JavaScript framework, simplifies the development of dynamic web applications. One common challenge developers face is managing navigation without the unsightly hash symbol (’’) in the URL. Traditional AngularJS routing relies on the hash, creating URLs like example.com//home. However, modern web applications demand cleaner, more user-friendly URLs such as example.com/home. Achieving AngularJS routing without the hash ‘’ requires understanding the framework’s configuration options and server-side adjustments. This article provides a comprehensive guide to removing the hash from AngularJS URLs, enhancing user experience, and improving SEO. We’ll delve into the necessary steps, best practices, and potential pitfalls to ensure a smooth implementation of hashless routing in your AngularJS applications.

Understanding AngularJS Routing with and without the Hash

AngularJS’s default routing mechanism utilizes the hash fragment () in the URL. This approach has historical roots, primarily to avoid full page reloads when navigating between different views within a single-page application (SPA). When a user clicks a link with a hash, the browser only updates the part of the URL after the hash, triggering AngularJS’s $route service to load the corresponding view. This mechanism works without requiring server-side configuration, making it a quick and easy solution for basic routing needs. However, the hash symbol can be visually unappealing and can negatively impact SEO, as search engines may not effectively index URLs containing fragments.

Removing the hash symbol involves leveraging AngularJS’s $locationProvider service and configuring the server to correctly handle requests for routes that don’t contain a hash. Specifically, you’ll need to enable HTML5 mode in $locationProvider. This mode allows AngularJS to use the browser’s history API (pushState and replaceState) to manipulate the URL without causing a full page reload. It effectively creates “clean” URLs that look like standard website URLs. The key benefit is a better user experience. Clean URLs are more memorable, easier to share, and generally contribute to a more professional appearance for your web application. Furthermore, it can improve your website’s SEO by making URLs more readable to search engine crawlers.

However, enabling HTML5 mode introduces a crucial server-side dependency. The server must be configured to serve the AngularJS application’s main entry point (e.g., index.html) for all routes defined in your AngularJS application. Without this server-side configuration, users navigating directly to a “clean” URL (e.g., example.com/about) will receive a 404 error because the server doesn’t know how to handle that specific route directly. This is where proper server configuration becomes essential for successful AngularJS routing without the hash ‘’.

Configuring AngularJS for Hashless Routing

To implement hashless routing, you need to modify your AngularJS application’s configuration. The primary tool for this is the $locationProvider service. Here’s a step-by-step guide:

  1. Inject $locationProvider into your application’s configuration block: Ensure your module’s config function receives $locationProvider as a dependency.
  2. Enable HTML5 mode: Use $locationProvider.html5Mode(true); to activate HTML5 mode. This is the core step to remove the hash.
  3. Set requireBase to false: If your application doesn’t have a tag in the section of your HTML, you might need to set requireBase to false using $locationProvider.html5Mode({ enabled: true, requireBase: false });. This prevents AngularJS from throwing an error if the base tag is missing.
  4. Configure the tag: If you choose to use the tag, ensure it’s correctly set to your application’s root URL. This helps AngularJS resolve relative URLs correctly. For example: .

Here’s an example of how this configuration might look in your AngularJS application:

angular.module('myApp', ['ngRoute']) .config(['$routeProvider', '$locationProvider', function($routeProvider, $locationProvider) { $routeProvider .when('/home', { templateUrl: 'views/home.html', controller: 'HomeController' }) .when('/about', { templateUrl: 'views/about.html', controller: 'AboutController' }) .otherwise({ redirectTo: '/home' }); $locationProvider.html5Mode({ enabled: true, requireBase: false }); }]); 

This code snippet demonstrates enabling HTML5 mode and configuring routes using $routeProvider. The $locationProvider.html5Mode() function is crucial for AngularJS routing without the hash ‘’. Remember to inject $locationProvider and $routeProvider as dependencies within your config block. You may need to adjust the requireBase property depending on your specific application setup. Make sure you understand the implications of setting requireBase to false, as it can affect how AngularJS handles relative URLs.

Server-Side Configuration for Hashless AngularJS Routing

Enabling HTML5 mode in AngularJS is only half the battle. The server must be configured to handle requests for your AngularJS routes. Without proper server-side configuration, users will encounter 404 errors when trying to access routes directly (e.g., by typing the URL into the browser or following a direct link). The server needs to be configured to serve your index.html file for any route that isn’t a static asset (like CSS, JavaScript, or images).

The specific configuration steps will vary depending on the server you are using. For example, if you are using Apache, you can use the .htaccess file to rewrite all requests to index.html. A typical .htaccess configuration might look like this:

<IfModule mod_rewrite.c> RewriteEngine On RewriteBase / RewriteRule ^index\.html$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.html [L] </IfModule> 

This configuration tells Apache to rewrite all requests that aren’t for existing files or directories to index.html. This ensures that AngularJS can handle the routing on the client-side. Similar configurations exist for other web servers like Nginx, Node.js (with Express), and IIS. For Nginx, you might use a configuration similar to this: try_files $uri $uri/ /index.html;. The key is to ensure that the server serves your AngularJS application for all client-side routes. Understanding these server configurations is vital for effectively implementing AngularJS routing without the hash ‘’.

Here are some important considerations for server configuration:

  • Static Asset Handling: Ensure your server correctly serves static assets like CSS, JavaScript, and images. These should not be rewritten to index.html.
  • Environment-Specific Configuration: Use different server configurations for development, staging, and production environments to avoid unexpected behavior.
  • Security Considerations: Always follow security best practices when configuring your server to prevent vulnerabilities.

Best Practices and Troubleshooting

Implementing AngularJS routing without the hash ‘’ can sometimes present challenges. Here are some best practices and troubleshooting tips to help you avoid common pitfalls:

Base Tag Configuration: A correctly configured tag is crucial. If you’re experiencing issues with routing, double-check that the href attribute of the tag is set to the correct root URL of your application. Incorrect base tag configuration can lead to broken links and routing errors. This is a very common mistake and often overlooked.

Server Configuration Verification: After configuring your server, thoroughly test your application by navigating to different routes directly in the browser. Ensure that you can access all routes without encountering 404 errors. Use browser developer tools to inspect network requests and identify any issues with server responses. Tools like Postman can also be helpful in testing your server configuration.

URL Rewriting Issues: URL rewriting can sometimes interfere with other server configurations or modules. If you encounter unexpected behavior, carefully review your server’s configuration files and ensure that the rewriting rules are not conflicting with other settings. Consult your server’s documentation for specific guidance on URL rewriting.

Here are some useful troubleshooting steps:

  • Check Browser Console: Examine the browser’s developer console for any JavaScript errors or warnings related to routing.
  • Inspect Network Requests: Use the network tab in the developer tools to analyze the requests being made by your application and identify any issues with server responses.
  • Simplify Configuration: Temporarily simplify your server configuration to isolate the source of the problem. For example, try removing other modules or rewriting rules to see if they are interfering with AngularJS routing.

Featured Snippet Optimization: To summarize, for AngularJS routing without the hash, enable HTML5 mode in $locationProvider by setting html5Mode(true). This leverages the browser’s history API for cleaner URLs. Crucially, configure your server to serve the AngularJS application’s entry point (e.g., index.html) for all defined routes, preventing 404 errors when users directly access these URLs. Proper server configuration, like using .htaccess in Apache or try_files in Nginx, ensures that all client-side routes are handled by the AngularJS application.

Infographic here
FAQ: AngularJS Routing without the Hash ---------------------------------------
**Q: Why remove the hash from AngularJS URLs?**
A: Removing the hash improves user experience by providing cleaner, more readable URLs. It also enhances SEO, as search engines can better index hashless URLs. [Learn more about AngularJS](https://courthousezoological.com/n7sqp6kh?key=e6dd02bc5dbf461b97a9da08df84d31c).
**Q: What is HTML5 mode in AngularJS?**
A: HTML5 mode allows AngularJS to use the browser's history API (pushState and replaceState) to manipulate the URL without causing a full page reload, creating "clean" URLs.
**Q: What server configuration is required for hashless routing?**
A: The server must be configured to serve the AngularJS application's main entry point (e.g., index.html) for all routes defined in the application. [Apache .htaccess configuration](https://www.digitalocean.com/community/tutorials/how-to-configure-apache-to-use-htaccess-files) is a common method.
**Q: What if I don't have access to server configuration?**
A: If you don't have access to server configuration, you cannot fully remove the hash. You will be limited to using the default AngularJS routing with the hash symbol.
**Q: What are common issues when implementing hashless routing?**
A: Common issues include incorrect base tag configuration, server misconfiguration leading to 404 errors, and conflicts with other server modules or rewriting rules. [Consult browser history API documentation](https://developer.mozilla.org/en-US/docs/Web/API/History) for more info.
By carefully following these guidelines and troubleshooting tips, you can successfully implement hashless routing in your AngularJS applications, improving user experience and SEO. Remember to thoroughly test your configuration and address any issues that arise during the implementation process. Consider using a reputable hosting provider with support for URL rewriting to simplify the server configuration process. [Refer to the official AngularJS documentation](https://angularjs.org/) for the most up-to-date information and best practices.

Implementing AngularJS routing without the hash ‘’ offers a significant upgrade to your application’s usability and search engine visibility. While it requires careful configuration of both the AngularJS application and the server, the benefits of cleaner URLs and a better user experience are well worth the effort. Ready to take your AngularJS application to the next level? Start by implementing the steps outlined in this guide, and don’t hesitate to explore further resources and documentation to fine-tune your configuration. Consider exploring related topics like AngularJS SEO best practices and advanced routing techniques to further optimize your web application.

Question & Answer :
I’m learning AngularJS and there’s one thing that really annoys me.

I use $routeProvider to declare routing rules for my application:

$routeProvider.when('/test', { controller: TestCtrl, templateUrl: 'views/test.html' }) .otherwise({ redirectTo: '/test' }); 

but when I navigate to my app in browser I see app/#/test instead of app/test.

So my question is why AngularJS adds this hash # to urls? Is there any possibility to avoid it?

In fact you need the # (hashtag) for non HTML5 browsers.

Otherwise they will just do an HTTP call to the server at the mentioned href. The # is an old browser shortcircuit which doesn’t fire the request, which allows many js frameworks to build their own clientside rerouting on top of that.

You can use $locationProvider.html5Mode(true) to tell angular to use HTML5 strategy if available.

Here the list of browser that support HTML5 strategy: http://caniuse.com/#feat=history