Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 514
Bartosz Dabrowski Avatar asked Dec 06 '25 06:12

Bartosz Dabrowski


1 Answers

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;
    }
}
like image 139
Dan Kanze Avatar answered Dec 07 '25 20:12

Dan Kanze