Still getting 'Not Found' when manually refreshing with angular.js route

Jay picture Jay · Nov 22, 2013 · Viewed 14.6k times · Source

After reading a ton of write-ups and stackoverflow questions on Angular.js route, I'm still getting the 'Not Found' error when I do a manual refresh.

Steps:

  1. browse to localhost --> because of my configuration (below), I'm taken to localhost/home. Views and everything load fine.
  2. hit refresh in the browser --> browser displays this Not Found: the requested /home is not found on this server

This question is probably most like Refreshing page gives "Page not found"

My configuration

// Routing configuration.
angular.module('myModule')
    .config(['$routeProvider', '$locationProvider', 
    function ($routeProvider, $locationProvider) {
        // Enable pushState in routes.
        $locationProvider.html5Mode(true);

        $routeProvider
            .when('/home', {
                templates: {
                    layout: '/views/home.html'
                },
                title: 'Welcome!'

            })
            .when('/launchpad', {
                templates: {
                    layout: '/views/layouts/default.html',
                    content: '/views/partials/profile.html'
                },
                title: "Launchpad"
            })
            .otherwise({
                redirectTo: '/home'
            });

    }
]);

Other things I have done:

  • In my index.html, I already have the <base href="/">
  • upgraded to angular 1.2.1

Here are the htaccess rules I have tried. None work.

from Refreshing page gives "Page not found"

<ifModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} !index
    RewriteCond %{REQUEST_URI} !.*\.(css|js|html|png) #Add extra extensions needed.
    RewriteRule (.*) index.html [L]
</ifModule>

from http://ericduran.io/2013/05/31/angular-html5Mode-with-yeoman/

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)       /index.html/#!/$1 
</IfModule>

Answer

Emir Herrera picture Emir Herrera · Aug 26, 2014

This .htaccess setting worked well for me

<IfModule mod_rewrite.c>
    Options +FollowSymlinks
    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_URI} !^/$
    RewriteRule (.*) /#!/$1 [NE,L,R=301]
</IfModule>