How to remove # from url angularjs

It’s possible to remove # from angularjs ?

Add Comment

  • 1 Answer(s)

    Yes it’s possible

    Here is the code and explanation

    Step 1:
    Create the .htaccess file and add below code

    RewriteEngine on

    # Don't rewrite files or directories
    RewriteCond %{REQUEST_FILENAME} -f [OR]
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule ^ - [L]

    # Rewrite everything else to index.html to allow html5 state links
    RewriteRule ^ index.html [L]

    Step 2

    Open your index.html file and add below code inside tag

    <base href="/">

    If you have using sub folder like http://localhost/shop use code like below

    <base href="/shop/">

    Step 3

    Add below code for config function

    enabled: true,
    requireBase: false

    The final app code is for example

    (function () {
    'use strict';

    .module('app', ['ngRoute', 'ngCookies'])

    config.$inject = ['$routeProvider', '$locationProvider'];
    function config($routeProvider, $locationProvider) {

    .when('/', {
    controller: 'HomeController',
    templateUrl: 'home/home.view.html',
    controllerAs: 'vm'

    .when('/login', {
    controller: 'LoginController',
    templateUrl: 'authentication-service/login/login.view.html',
    controllerAs: 'vm'

    .when('/register', {
    controller: 'RegisterController',
    templateUrl: 'authentication-service/register/register.view.html',
    controllerAs: 'vm'

    .otherwise({ redirectTo: '/login' });

    enabled: true,
    requireBase: false

    run.$inject = ['$rootScope', '$location', '$cookies', '$http'];
    function run($rootScope, $location, $cookies, $http) {
    // keep user logged in after page refresh
    $rootScope.globals = $cookies.getObject('globals') || {};
    if ($rootScope.globals.currentUser) {
    $http.defaults.headers.common['Authorization'] = 'Basic ' + $rootScope.globals.currentUser.authdata;

    $rootScope.$on('$locationChangeStart', function (event, next, current) {
    // redirect to login page if not logged in and trying to access a restricted page
    var restrictedPage = $.inArray($location.path(), ['/login', '/register']) === -1;
    var loggedIn = $rootScope.globals.currentUser;
    if (restrictedPage && !loggedIn) {


    Answered on March 23, 2017.
    Add Comment

  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.