3

I am having issues with routing in my Angular app when a url is visited directly via the address bar, or linked to directly from elsewhere - all addresses revert to the home page.

However, routes work fine when visited via a link in the app.

There are already a few similar questions on this

AngularJS HTML5Mode

Angular routing doesn't work when URL is directly visited in browser but works when clicked to?

As I understand it, the Angular code does not run when visited directly as the page renders before the app code runs, therefore server side rewrites need to happen pointing the browser at index.html at which point the app runs and the routes get picked up.

I am using Firebase hosting, which has rewrite rules.

However I have followed the fixes listed above, and elsewhere on the site, and I am still having issues.

My routes look like this:

app.js

.config(function($routeProvider, $locationProvider) {
    $routeProvider
        .when('/', {
            templateUrl: 'views/posts.html',
            controller: 'PostsCtrl'
        })
        .when('/posts/:postId', {
            templateUrl: 'views/postview.html',
            controller: 'PostViewCtrl'
        })
        .when('/new', {
            templateUrl: 'views/new-post.html',
            controller: 'AddPostCtrl'
        })
        .when('/register', {
            templateUrl: 'views/register.html',
            controller: 'AuthCtrl',
            resolve: {
                user: function(Auth) {
                    return Auth.resolveUser();
                }
            }
        })
        .when('/login', {
            templateUrl: 'views/login.html',
            controller: 'AuthCtrl',
            resolve: {
                user: function(Auth) {
                    return Auth.resolveUser();
                }
            }
        })
        .when('/edit-profile', {
            templateUrl: 'views/edit-profile.html',
            controller: 'EditProfileCtrl',
            resolve: {
                user: function(Auth) {
                    return Auth.resolveUser();
                }
            }
        })
        .when('/change-password', {
            templateUrl: 'views/change-password.html',
            controller: 'ChangePasswordCtrl',
            resolve: {
                user: function(Auth) {
                    return Auth.resolveUser();
                }
            }
        })
        .when('/@:userId', {
            templateUrl: 'views/profile.html',
            controller: 'ProfileCtrl',
            resolve: {
                user: function(Auth) {
                    return Auth.resolveUser();
                }
            }
        })
        .when('/users/:userId', {
            templateUrl: 'views/profile.html',
            controller: 'ProfileCtrl',
            resolve: {
                user: function(Auth) {
                    return Auth.resolveUser();
                }
            }
        })
        .when('/search', {
            templateUrl: 'views/search.html',
            controller: 'SearchCtrl'
        })
        .otherwise({
            redirectTo: '/'
        });
    $locationProvider.html5Mode(true);
})

My firebase rewrite rules are as follows

firebase.json

"rewrites": [ {
    "source": "**",
    "destination": "/index.html"
          } ]

I also have <base href="/"> in my <head> as outlined in the Angular docs for the HTML5 Mode

I have contacted Firebase and they have confirmed that the rewrites are working correctly on the app.

NOTE: I should also say that I am also having this issue when running the app locally without the HTML5 Mode (so with /#/ in the URL).

What am I doing wrong? I'm assuming it must be something to do with my routing. I have already tried changing my 'otherwise' to go to another route to see if that is where this error is being picked up, but it still reverts back to the home page.

Any help here would be hugely appreciated, many thanks.

6
  • what is the URL that you're trying in address bar ? Commented Jul 17, 2015 at 20:39
  • @Arkantos it's any URL in the app, so for example when I click on a link in the app that goes to '/login' it works fine, but if I type 'myapp.com/login' in to the address bar it redirects to the home page. Commented Jul 17, 2015 at 20:41
  • So basically the rewrite is needed to let direct links work with firebase and remove the #? Commented Oct 26, 2015 at 0:00
  • 1
    @JohnAndrews yeah it works well and is pretty easy to implement, I just messed up elsewhere in my app - see answer below Commented Oct 26, 2015 at 8:16
  • 1
    @JohnAndrews I found this NPM module, firebase http server, that essentially runs your local environment as if it was on firebase. Otherwise, yes you're right you'd have to turn the rewrites off for local development Commented Oct 26, 2015 at 8:20

1 Answer 1

3

Yeah, so I'm an idiot. I found a rogue piece of code in a service that was redirecting to '/'. My apologies

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.