How To Remove Hash From Application URL In AngularJS


Angular is a framework, which is developed and maintained by Google. It has features such as Two-Way binding, Dependency Injection, Testing and Directives. It is widely used to create a Single Page Application(SPA) with Two-Way binding.

To find more, refer the URL

Going further, I assume that you are aware of how to use Angular routing, if not refer Angular routing tutorial

Why does URL show Hash prefix?

If you are using Angular v1.5 or lesser version, the app URL will show /#/ and from v1.6; it changes to /!#/. When you are using AngularJS routing, it rewrites the URL and shows # in the URL, so by default URL's are shown below.

  • http://localhost:51287/#/Home
  • http://localhost:51287/#/AboutUs
  • http://localhost:51287/#/ContactUs

Where the path that maps to the folder is removed and # is placed. It is very easy to remove # from the URL.

Only these two things are to be added to make it work.

  • Configuration if $locationProvider.
  • Setting base path for relative location of pages.

$location Service

The $location Service parses the URL in the Browser address bar, which is based on the window.location and makes the URL available to your Application.

Changes to the the URL in the address bar are reflected into $location service and changes to $location are reflected into the Browser address bar.

I would highly recommend reading through the official Angular $location docs before moving forward.

Base Tag

Base tag specifies a default URL for all the relative URL's in the Application. There can be only one <base> tag in a document and it is written in <head> section of an HTML. It is written, as shown below:

<base href = “” />

Configuring $locationProvider

Let’s start by configuring $locationprovider. 

  1. DemoApplicationModule.config(['$routeProvider''$locationProvider'function($routeProvider, $locationProvider) {  
  2.     $routeProvider.  
  3.     when('/Home', {  
  4.         templateUrl: ApplicationURL + '/AngularJs/Views/Home.html'  
  5.     }).when('/AboutUs', {  
  6.         templateUrl: ApplicationURL + '/AngularJs/Views/AboutUs.html'  
  7.     }).when('/ContactUs', {  
  8.         templateUrl: ApplicationURL + '/AngularJs/Views/ContactUs.html'  
  9.     }).otherwise({  
  10.         redirectTo: '/Home'  
  11.     });  
  12.     // for enabling html5 mod  
  13.     $locationProvider.html5Mode(true);  
  14. }]); // This is just a sample script. Paste your real code (javascript or HTML) here.  

In the code given above, we have configured html5mode.

What is html5mode? It is a standardized way for the manipulation of the Browser URL, so it helps an Angular to change the URL without refreshing the page.

If you are using newer version of an Angular, then you can directly write

  1. $locationProvider.html5Mode(true);   

to enable html5mode.

Enabling Base URL (Relative URLs)

To set the base URL in an Application, we must write <base> tag in the head section of an HTML. If the HTML pages, which are to be viewed are in the root path, then we do not have to mention the relative path to the pages and we can simply write <base href="/"> to map the pages.

If the pages are in different folder like the example, then we should provide the relative path to the folder, as shown below.

  1. <base href="/Demo/Index/">   

This will set the view folder to demo/index and render the pages from this path.


  1. <!DOCTYPE html>  
  2. <html>  
  4. <head>  
  5.     <meta charset="utf-8" />  
  6.     <meta name="viewport" content="width=device-width" />  
  7.     <title>Routing Demo</title>  
  8.     <script src="~/Scripts/angular.js"></script>  
  9.     <script src="~/Scripts/angular-route.js"></script>  
  10.     <script type="text/javascript">  
  11.         var ApplicationURL = '@WebConfigurationManager.AppSettings["ApplicationURL"]';  
  12.     </script>  
  13.     <script src="~/AngularJS/Modules/DemoModule.js"></script>  
  14.     <!--provide base url for mapping routing in the angular routing-->  
  15.     <base href="/Demo/Index/"> </head>  
  17. <body ng-app="DemoApplicationModule"> <a href="./#/Home">Home</a> <a href="./#/AboutUs">AboutUs</a> <a href="./#/ContactUs">ContactUs</a> @RenderBody() </body>  
  19. </html>   


  1. // declared module.  
  2. var DemoApplicationModule = angular.module("DemoApplicationModule", ['ngRoute']);  
  3. //for routing of the pages  
  4. DemoApplicationModule.config(['$routeProvider''$locationProvider'function($routeProvider, $locationProvider) {  
  5.     $routeProvider.  
  6.     when('/Home', {  
  7.         templateUrl: ApplicationURL + '/AngularJs/Views/Home.html'  
  8.     }).when('/AboutUs', {  
  9.         templateUrl: ApplicationURL + '/AngularJs/Views/AboutUs.html'  
  10.     }).when('/ContactUs', {  
  11.         templateUrl: ApplicationURL + '/AngularJs/Views/ContactUs.html'  
  12.     }).otherwise({  
  13.         redirectTo: '/Home'  
  14.     });  
  15.     // for enabling html5 mod  
  16.     $locationProvider.html5Mode(true);  
  17. }]);   


This is the simplest way to remove Hash (#) from the URL and get a pretty URL.