Bartosz Dabrowski
Bartosz Dabrowski

Reputation: 1860

AngularJS HTML5 nested routes

Angular: 1.1.5

I go to: http://xxxxx.com/route1/route2/route3 and /route3 gets triggered instead of route1/route2/route3 Works fine with HTML5 mode off ... :(

Nginx config

location / {
    index index.html;
    try_files $uri $uri/ /index.html?$args;
}

My app config

$routeProvider.when('/route1/route2/route3', {
        templateUrl: '/views/home.html',
        controller: 'HomeCtrl'
    });

$routeProvider.when('/route3', {
        templateUrl: '/views/home.html',
        controller: 'HomeCtrl'
    });

$locationProvider.hashPrefix('!');
$locationProvider.html5Mode(true);

$location Log:

$$protocol: "http", $$host: "xxxxxx"…}
$$absUrl: "http://xxxxxxx.com/route1/route2/route3"
$$hash: ""
$$host: "10.44.11.73"
$$path: "/route3"
$$port: 80
$$protocol: "http"
$$replace: false
$$url: "/route3"

How can I change my settings to make HTML5 urls working with nested routes?

Upvotes: 3

Views: 2332

Answers (2)

Elnur Abdurrakhimov
Elnur Abdurrakhimov

Reputation: 44831

Add

<base href="/">

to your index.html.

Upvotes: 3

Dan Kanze
Dan Kanze

Reputation: 18595

For single page apps, this is all you need in your NGINX config:

server
{
    server_name example.com *.example.com;
    access_log /var/log/nginx/example.com.access.log;
    error_log /var/log/nginx/example.com.error.log;
    root /var/www/html/example;
    index index.html;
    location / {
       try_files $uri /index.html;
    }
}

Upvotes: 3

Related Questions