Skip to content

Instantly share code, notes, and snippets.

@serweb-labs
Last active February 24, 2016 00:22
Show Gist options
  • Save serweb-labs/696d2f76024cdacd4c61 to your computer and use it in GitHub Desktop.
Save serweb-labs/696d2f76024cdacd4c61 to your computer and use it in GitHub Desktop.

AngularJS: Prety URLs without hashbang

####Add html5 mode on your app config, for example:

angular
  .module('myappApp', [])
  .config(function ($routeProvider, $locationProvider) {
    $routeProvider
      .when('/', {
        templateUrl: 'views/main.html',
        controller: 'MainCtrl',
        controllerAs: 'main'
      })
      .when('/about', {
        templateUrl: 'views/about.html',
        controller: 'AboutCtrl',
        controllerAs: 'about'
      })
      .otherwise({
        redirectTo: '/'
      });

      $locationProvider.html5Mode(true); //<--- add this code here
  });

Now you need configure your server for whath all routes land in index.html

In apache

After doing this, you need to create a .htaccess file on the root of your shared Apache server.

RewriteEngine On  
  # If an existing asset or directory is requested go to it as it is
  RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR]
  RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d
  RewriteRule ^ - [L]

  # If the requested resource doesn't exist, use index.html
  RewriteRule ^ /index.html

In Grunt Server

Make sure you have the following declared at the top of your grunt file:

var modRewrite = require('connect-modrewrite');

in 'connect' object:

livereload: {
    options: {
        open: true,
        middleware: function (connect) {
            return [
                modRewrite(['^[^\\.]*$ /index.html [L]']),
                connect.static('.tmp'),
                connect().use(
                    '/bower_components',
                    connect.static('./bower_components')
                ),
                connect.static(appConfig.app)
            ];
        }
    }
},
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment