Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have some routes for my application:

$routeProvider.
            when('/user', {templateUrl: 'user/partials/userlist.html',   controller: 'userListController'}).
            when('/user/:userName', {templateUrl: 'user/partials/userdetail.html', controller: 'userDetailController'}).
            when('/group',{templateUrl: 'group/partials/grouplist.html', controller: 'groupListController'}).
            when('/group/:groupName', {templateUrl: 'group/partials/groupdetail.html', controller: 'groupDetailController'}).
            when('/category',{templateUrl: 'category/partials/categorylist.html', controller: 'categoryListController'}).
            otherwise({redirectTo: '/user'});

the root of the application is /admin/ and I have my .htaccess set to default to index.html so the site looks like: www.domain.com/admin/#/user/username...etc... in the non HTML5 version.

Everything works fine when it's like that. When I turn on HTML 5 it redirects immediately to: www.domain.com/user/ and of course, it doesn't work. How can I tell my routing provider that the root of my module is /admin/?

EDIT:

I added: <base href="/admin/"></base> to the last line of my index.html head tags and now when I go to www.domain.com/admin it correctly redirects to www.domain.com/admin/user and the userlist template and controller are used. However, when I navigate directly to www.domain.com/admin/user it gives me a page not found error. I'm starting to wonder if this part of it has to do with my default rewrite rule:

RewriteRule ^$ index.html

EDIT:

Yup. Figured it out. Took out the Rewrite entirely and it worked for from the default redirect to user and then going to userdetail it worked, however typing in /user or any other route doesn't work...it seems like it only works with html5 is when I do the navigation from the application itself, I can't navigate directly to the different views. for instance when I try to go to group directly it doesn't work.

So, no rewrite rule to rewrite to index, the user default route works, group and category don't.

EDIT:

Here's what I've found. With no rewrite for the default index.html, the $location.html5mode(true) set, it works so long as when you navigate to the page you type in www.domain.com/#/user/ or www.domain.com/#/group. it immediately rewrites the URL as www.domain.com/user or www.domain.com/group. but if you try to navigate to www.domain.com/user directly it doesn't work. obviously, rewrite rules won't help here because it's the front end doing the processing of the route.

Is this standard behavior for angular routing?

EDITED TO ADD TO THE SOLUTION:

If you have any additional routes in place that redirect to subdirectories, besure to either use the [END] tag or make sure the directories have mod-rewrite turned off or it will process the generic rule again after the rewrite and everything will redirect to the index.

share|improve this question
2  
Have you tried setting a base href in your index.html page? –  NuclearGhost Oct 14 '13 at 14:03
    
That fixed the otherwise redirect, but now everything else is broken. –  Snowburnt Oct 14 '13 at 14:21
1  
$locationprovider.html5mode(true)? –  Henk Jansen Oct 14 '13 at 14:29
    
That is set and everything breaks, when it isn't set everything works, albeit with that hash sign, which I'm trying to get rid of. –  Snowburnt Oct 14 '13 at 14:32
    
RE: Henk Jansen $locationProvider.html5Mode(true); rather than $locationprovider.html5mode(true); –  rdjs Mar 11 at 21:50
add comment

1 Answer

up vote 0 down vote accepted
+100

From what I can see, the problem is you are only serving the AngularJS HTML and JS when people go to the root url. The server doesn't see hashes it only sees www.domain.com/. However it sounds like you want Angular to do everything so you should consider a wildcard route like this.

RewriteRule ^.*$ index.html

This will ensure that no matter what cones after www.domain.com/ you will return index.html to the browser which will load AngularJS and your routing so that AngularJS can do the rest.

Unless I'm misunderstanding what's happening here I'm pretty sure this is your problem.

share|improve this answer
    
That did it, the (\w+) was too specific, it didn't grab any extra directories. –  Snowburnt Nov 3 '13 at 16:38
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.