Skip to content Skip to sidebar Skip to footer

How Do I Implement `$locationprovider.html5mode(true);

From what I understand doesn't $locationProvider.html5Mode(true); remove the hash from your URL? While it does that for me only the home page seems to be ok when you refresh the pa

Solution 1:

This is because the webserver will handle the request first, so if you don't have URL Rewriting in any form set up it will just look for a folder in the webserver with the name you have put in your 'route'.

If you're using a Node Express webserver, you can set the rewrites like this:

app.all('/*', function(req, res) {
    res.sendfile(__dirname + '/index.html'); // Or whatever your public folder is
});

For Apache servers you could use a .htaccess, with something like this:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)       /index.php/#/$1 // Might have to fiddle with the hash and/or other prefix you want to use
</IfModule>

Solution 2:

From the docs on $location, it says that you need to have server-side rewriting for HTML5 mode to work with page refreshes. This page details the different server configurations.

Post a Comment for "How Do I Implement `$locationprovider.html5mode(true);"