Search code examples
javascriptangularjsurl-routing

Angular url routing doesn't work on direct visit


I've set up routing on my project using AngularJS to load a different template when a menu button is clicked. The template is based on browser URL. Only problem is that the template will not load when the URL is visited directly. Instead it will load a blank page (object not found). How to overcome this?

So the project is on local drive in folder /project4. There are three menu buttons, two of them linking to:

project4/about and project4/contact

This is the HTML section:

<html>
<head>
    <base href="/ordina/angular/project4/">
    <link href=
    "//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" rel=
    "stylesheet">
    <link href=
    "//netdna.bootstrapcdn.com/font-awesome/4.0.0/css/font-awesome.css" rel=
    "stylesheet">
    <script src=
    "https://ajax.googleapis.com/ajax/libs/angularjs/1.2.25/angular.min.js"></script>
    <script src=
    "//ajax.googleapis.com/ajax/libs/angularjs/1.2.25/angular-route.js"></script>
    <script src="script.js"></script>

    <title></title>
</head>

<body>
    <header>
        <nav class="navbar navbar-default">
            <div class="container">
                <div class="navbar-header">
                    <a class="navbar-brand" href="/">Angular Routing
                    Example</a>
                </div>

                <ul class="nav navbar-nav navbar-right">
                    <li>
                        <a href=""><i class="fa fa-home"></i> Home</a>
                    </li>

                    <li>
                        <a href="about"><i class="fa fa-shield"></i> About</a>
                    </li>

                    <li>
                        <a href="contact"><i class="fa fa-comment"></i>
                        Contact</a>
                    </li>
                </ul>
            </div>
        </nav>
    </header>

    <div id="main">
        <div>
            <p>This text is not being shown in the frontend.</p>
        </div>
    </div>
</body>
</html>

This is the .js file:

// script.js

    // create the module and name it scotchApp
    var scotchApp = angular.module('scotchApp', ['ngRoute']);

    // configure our routes
    scotchApp.config(function ($logProvider, $routeProvider, $locationProvider) {

        $logProvider.debugEnabled(true);

        $locationProvider.html5Mode(true);

        $routeProvider

            // route for the home page
            .when('/', {
                templateUrl : 'pages/home.html',
                controller  : 'mainController',
                controllerAs: 'home'
            })

            // route for the about page
            .when('/about', {
                templateUrl : 'pages/about.html',
                controller  : 'aboutController',
                controllerAs: 'about'
            })

            // route for the contact page
            .when('/contact', {
                templateUrl : 'pages/contact.html',
                controller  : 'contactController',
                controllerAs: 'contact'
            });

        //$locationProvider.hashPrefix('!');




    });

    // create the controller and inject Angular's $scope
    scotchApp.controller('mainController', function($scope) {
        // create a message to display in our view
        $scope.message = 'Everyone come and see how good I look!';
    });

    scotchApp.controller('aboutController', function($scope) {
        $scope.message = 'Look! I am an about page.';
    });

    scotchApp.controller('contactController', function($scope) {
        $scope.message = 'Contact us! JK. This is just a demo.';
    });

Solution

  • You have html5mode turned on. In order to make the GET request for the URL work you have to implement a URL rewrite to the entry point of your AngularJS application first. (This is done Server Side in .htaccess)

    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} -s [OR]
    RewriteCond %{REQUEST_FILENAME} -l [OR]
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule ^.*$ - [NC,L]
    
    RewriteRule ^(.*) /index.html [NC,L]
    

    Edit: htaccess in XAMPP

    1. Create htaccess.txt and paste content into it
    2. On Windows do: Start Run > cmd
    3. then rename c:\pathtoyourhtaccessfile\htaccess.txt .htaccess
    4. You might have to enable htaccess files in your httpd.conf (threw XAMPP Control Panel). Look for this Line: AllowOverride All It has to be set to "All", not "None" or sth else
    5. Restart Apache