1

I create a simple angularjs app.

this is my config:

app.config(function ($routeProvider, $locationProvider)
{
    $routeProvider.when("/home", {
        controller: "homeController",
        templateUrl: "/views/home.html"
    });
    $routeProvider.when("/login", {
        controller: "loginController",
        templateUrl: "/views/login.html"
    });
    $routeProvider.otherwise({ redirectTo: "/home" }); // or notfound.html
    $locationProvider.html5Mode(true);
});

and in my index.html

<base href="/">

when I start web site from index.html (localhost/index.html) file everything is fine. the url change to localhost/home or localhost/login and everythings work.

but when I use the home or login url I get 404 error.

2 Answers 2

0

You need to get the server involved to handle 404 errors.

http://localhost/home when asked by the browser looks up the home path on you server or home.html when it isn't found you get the 404

When you select a link within angular app it will load index.html (the app) and route to #/home then re-address it to /home

You can set up your server to route all traffic to / and let the app handle routing.

look here for a similar solution. AngularJS Dynamic Routes Without Hashtag

edit

redirect 404 to main index Configuring custom ASP 404 page with redirect for IIS7 website

1
  • how can i set this in iis or iis express?
    – user495093
    Jan 16, 2015 at 20:25
0

If you're running without a web server, you will need to prefix your links with the # sign.

<a href="#/login">

AngularJS routing without a web server

2
  • first my link works. its when user insert url themself in browser. second when I type localhost/#login it works. nice suggestion. but not what I want
    – user495093
    Jan 16, 2015 at 20:25
  • 1
    @Raika I dont think it's possible to do what you're asking for. Remember that AngularJS is a client-side framework, doing it's job in JavaScript in the client browser. There's no actual server involved unless you install one.
    – Jim Aho
    Jan 16, 2015 at 20:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.