Angularjs routing throws 404 without #

2019-08-04 01:37发布

I am creating a website with AngularJS and I am using the ngRoute module to handle page requests.

In my app.config function I have setup the routing as follows:

app.config(function($routeProvider, $locationProvider, $httpProvider){
    $locationProvider.html5Mode(true);
    $routeProvider.when('/', {
        templateUrl: 'pages/index.html',
        controller: 'mainController'
    }).when('/testpage', {
        templateUrl: 'pages/test.html',
        controller: 'testController'
    }).otherwise({
        redirectTo: '/'
    });
});

When I try to access the test page, on my MAMP local server, by adding a # before the page name like this: http://dev.mysite.com:8888/#testpage everything works fine.

However, if I don't add the # before the page name, like this: http://dev.mysite.com:8888/testpage I get a 404 error.

I have added <base href="/"> to the <head> of my index.html page that contains the <div ng-view> tag.

I would very much appreciate it if someone could explain what I am doing wrong.

1条回答
Rolldiameter
2楼-- · 2019-08-04 02:04

Angular looks for the # symbol to handle routing. Everything after the # symbol is interpreted by the routing function. No # symbol, no angular route. If you can run in Html5 mode, then you can turn this off, but to work with older browsers, it is needed.

查看更多
登录 后发表回答