2

I want to remove # hash in angularJs using $locationProvider.html5Mode(true); but this is causing all URLs to be routed through angularJs. How can I set it up so only some pre-defined URLs got routed through angularJs, while the rest still using Laravel.

The code is:

    app.config(['$routeProvider', '$locationProvider',
        function($routeProvider, $locationProvider) {
            $routeProvider
            .when('/alls', {
                templateUrl: app.site_url + 'ang/index',
                controller: 'MainCtrl'
            })
            .when('/alls/page/:page', {
                templateUrl: app.site_url + 'ang/index',
                controller: 'MainCtrl'
            })
            .otherwise({
                redirectTo: '/alls'
            });
            $locationProvider.html5Mode(true);
        }
    ]);

So if it's not /alls or /alls/page/, Laravel should handle the routing.

4
  • Are you using any server side rewriting? Commented Oct 10, 2013 at 17:56
  • @Christian: Yes, other urls should go through index.php. I use htaccess to re-write: RewriteCond %{REQUEST_FILENAME} -s [OR] RewriteCond %{REQUEST_FILENAME} -l [OR] RewriteCond %{REQUEST_FILENAME} -d RewriteRule ^.*$ - [NC,L] RewriteRule ^.*$ index.php?/$1 [NC,L] Commented Oct 11, 2013 at 3:40
  • and from where do you serve the frontend? From the /public directory? Commented Oct 11, 2013 at 8:35
  • Yes, public directory. If I set html5Mode as false, they work fine, using: http://www.domain.com/alls/#/ http://www.domain.com/alls/#/page/10 With this setting, I can go to domain.com/about, and the page will refresh and display the content correctly. But I want to remove the # hash by setting html5Mode as true, like so: http://www.domain.com/alls/ http://www.domain.com/alls/page/10 But now, when I go to domain.com/about, the page won't refresh, and it's not displaying the content of about page. Commented Oct 11, 2013 at 15:55

1 Answer 1

3

I've tried using <base href="."> as explained in: http://docs.angularjs.org/guide/dev_guide.services.$location. But it doesn't work. I finally got it to work. Here is the solution:

 app.config(['$routeProvider', '$locationProvider',
    function($routeProvider, $locationProvider) {
        $routeProvider
        .when('/alls', {
            templateUrl: app.site_url + 'ang/index',
            controller: 'MainCtrl'
        })
        .when('/alls/page/:page', {
            templateUrl: app.site_url + 'ang/index',
            controller: 'MainCtrl'
        })
        .otherwise({
            templateUrl: app.site_url + 'ang/index',
             controller: function(){
                window.location.href = window.location.href;
            }               
        });           
        $locationProvider.html5Mode(true);
    }
]);

Hopefully it will help someone else.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.