2

I have an Angular app that I'm deploying in foobar.com/myapp directory on my webserver.

The build command I use is ng build -bh /myapp to make sure that my index.html contains the

<base href="myapp">

line. All the assets and required files load up fine. On the main page I have a router outlet and my routing setup is as follows:

const appRoutes: Routes = [
  { path: 'configure', component: ConfigureComponent },
  { path: 'dashboard',      component: DashboardComponent },
  { path: 'myapp',
    redirectTo: '/configure',
    pathMatch: 'full'
  },
  { path: '',
    redirectTo: '/configure',
    pathMatch: 'full'
  }
];

The problem I am facing is that if I open up the url http://foobar.com/myapp directly, I am landing on the ConfigureComponent as expected, but the URL gets rewritten to http://foobar.com/myapp/myapp/configure. On the ConfigureComponent, I have a link:

<a routerLink="/dashboard" class="btn btn-primary">Save and go to Dashboard</a>

When I click on it, it takes me to the DashboardComponent fine, but the URL is again http://foobar.com/myapp/myapp/dashboard.

I tried to play around with different route configs, but did not manage to set it up the way that eliminates this duplication in the URL. What I don't quite like here is that the app has to have the myapp subdirectory name hardcoded in it, although I would like to make it completely agnostic of the subfolder where I deploy it. (apart from that I have to specify it in the ng build -bh ....

What am I doing wrong here..?

4
  • when you deleting duplicate in your URL field it sends you to a right route? Commented Feb 22, 2018 at 19:59
  • No, it does not. If I manually switch to foobar.com/myapp/configure, it's 404 Commented Feb 22, 2018 at 20:13
  • What the route comes when you not specifying base href? Commented Feb 22, 2018 at 20:18
  • Actually this is not route config problem, because routing withing angular app have no influence on your base route. You need to look at directories path, and play with base href, because depending on server preferences app may be deployed with some baseUrl by its own Commented Feb 22, 2018 at 20:23

1 Answer 1

4

Base should have a traling slash. Try ng build -bh /myapp/

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

2 Comments

Thanks a lot. Would have been kind from ng build if there was a warning, or if it added that automatically :-)
Yeah :D It's totally valid without, but can't see why anyone would need one without.

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.