Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 6 route path not working for direct url when deployed in subdirectory

app.routing.module.ts

import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { HomeComponent } from './public/home/home.component';

const routes: Routes = [
  {
    path: 'customers',
    loadChildren: '../app/customers/customers.module#CustomersModule'
  },
  {
    path: 'admin',
    loadChildren: '../app/admin/admin.module#AdminModule'
  },
  {
    path: 'home',
    component: HomeComponent
  },
  {
    path: '**',    
    redirectTo: 'home',    
  }
];

@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule]
})
export class AppRoutingModule { }

These works fine under the Development environment. If I directly open localhost:4200/home then it's landing on the expected page.

However, the one built and deployed using with ng build --prod --base-href=/myngapp/ do works only on www.domain.com/myngapp if I open the www.domain.com/myngapp/home directly, then it gives 404 not found error.

like image 639
Amit Shah Avatar asked Jul 04 '18 11:07

Amit Shah


People also ask

Which Angular package is used to route to URL?

Generate an application with routing enabledlink The following command uses the Angular CLI to generate a basic Angular application with an application routing module, called AppRoutingModule , which is an NgModule where you can configure your routes.

How does Angular determine active routes?

Show activity on this post. You can check the current route by injecting the Location object into your controller and checking the path() , like so: class MyController { constructor(private location:Location) {} ...

What does routerLink directive do?

Linking Routes in HTMLTo add links to one of the routes, use the routerLink directive in HTML. This directive accepts an array. The first parameter is the name of the route, and the second parameter is the parameters that you want to pass with the route.

How do I get the current route in angular?

There are many ways by which you can get a current Route or URL in Angular. You can use the router service, location service or window object to get the path. You can also listen to changes to URL using the router event or URL change event of the location service. Inject the Router and use the URL property.

What is routing in Angular 6?

Angular 6 - Routing. Routing basically means navigating between pages. You have seen many sites with links that direct you to a new page. This can be achieved using routing. Here the pages that we are referring to will be in the form of components. We have already seen how to create a component.

Do I need to set up routing in angular CLI?

you need to setup something to redirect to your index.html page or change the default location strategy to hash. Yes - you do :) Angular CLI does not automatically add routing to the app just yet therefore you would need to modify app.module.ts file as example below:

How to get the current URL of an app in angular?

Import Location Service from the @angular/common. And use the Location.path () to get the current URL. You can also use the window object to get the path. But remember that it works only if the app is using the browser.


3 Answers

You need to setup proper URL rewriting rules. Angular docs explains how to do it for most popular http servers. You can find details here.

https://angular.io/guide/deployment#server-configuration

What it do, is to simply speaking, tell server to always serve index.html no matter what is the path of the request. eg.

www.domain.com/myngapp/home -> serve index.html, not home.html or something similar www.domain.com/myngapp/some/child/routing ->serve index.html and dont try to find some/child/routing.html or php and so on.

like image 59
Antoniossss Avatar answered Oct 17 '22 16:10

Antoniossss


You must configure your HTTP server(apache, Nginx or others) to redirect all URI which began with www.domain.com/myngapp/** to the index.html file.

Notice the routes of your application (https://angular6demoamit.000webhostapp.com/) works fine when they are accessed "inside Angular" by interaction with buttons and menus. However, when you try to access the route typing it directlly on the browser, the server doesn't know the URI https://angular6demoamit.000webhostapp.com/home is a route of an Angular application. It'll probabilly try to load the index page of an inexistent home directory.

There isn't a single configuration to solve your problem. It'll depend on your HTTP server and infraestructure. Read the guide of angular which describes some configurations examples to some kinds of HTTP servers: https://angular.io/guide/deployment#routed-apps-must-fallback-to-indexhtml.

like image 5
Rodrigo Avatar answered Oct 17 '22 15:10

Rodrigo


I had same issue. if it's working fine in localhost, then you should try this solution. Maybe adding URL rewriting rules in your .htaccess file solve your problem. I found it on http://joeljoseph.net/angular-6-deploy-on-apache-server-by-solving-404-not-found-error-on-page-refresh/

Add this into your .htaccess file.

RewriteEngine on

# Don't rewrite files or directories
RewriteCond %{REQUEST_FILENAME} -f [OR]  
RewriteCond %{REQUEST_FILENAME} -d  
RewriteRule ^ - [L]

# Rewrite everything else to index.html to allow html5 state links
RewriteRule ^ index.html [L]  

for more detail go to http://joeljoseph.net/angular-6-deploy-on-apache-server-by-solving-404-not-found-error-on-page-refresh/

like image 3
Rutul Dave Avatar answered Oct 17 '22 16:10

Rutul Dave