Search code examples
javascriptbackbone.jsbackbone-routing

How to make a static site made with Backbone respond to dynamic urls to the site


I am making a static site with Backbone that will have only one file as an entry point(index.html). I was wondering how to make the site respond to any external url to the site? For example

www.example.com/this_route www.example.com/search

While I do have a router set up now, I can only trigger url changes from within the app:

router.navigate( "to_here" );

But if i type www.example.com/to_here in the url bar of a browser, i get the error: "The requested URL /to_here was not found on this server."

Thanks for any help


Solution

  • You need to set up your web server to always respond with index.html.

    I'm using nginx for this purpose with rules below, it always serve index.html for any requests like this localhost:8080/to_here

    server {
        listen       8080;
        server_name  localhost;
    
        location / {
            root   /path/to/folder/with/files;
            try_files $uri /index.html;
        }
    }