0
votes

I'm new on Angular 5 and try to put routing following Angular 5 tutorial. I have few differences:

  • I try to simulate a production encirnoment haveing only an index.html point on all transpiled scripts.
  • Because I have only an index.html, I'm using a virtual directory with Wamp. So the url "http://clientpackages" will point on a local directory like a http server.

The routing works correctly when I navigate in the app (using routerLinks), but when I refresh the page or write directly the URL in the browser, I have a 404.

Because i'm not able to configure the routing of my virtual directory, how can I configure Angular to manage correctly the routing ?

I already following questions, but didn't find a good solution:

Index.html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>XXX</title>
    <base href="/">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="dist/assets/css/XXX.css">
    <link rel="stylesheet" href="dist/assets/vendor/css/bootstrap.min.css">
</head>
<body>
    <!-- Application -->
    <div class="container">
        <app>Dev loading...</app>
    </div>
    <!-- Scripts -->
    <script type="text/javascript" src="dist/runtime.js"></script>
    <script type="text/javascript" src="dist/polyfills.js"></script>
    <script type="text/javascript" src="dist/styles.js"></script>
    <script type="text/javascript" src="dist/vendor.js"></script>
    <script type="text/javascript" src="dist/main.js"></script>
    <script type="text/javascript" src="dist/assets/vendor/js/jquery-3.3.1.min.js"></script>
    <script type="text/javascript" src="dist/assets/vendor/js/bootstrap.min.js"></script>
</body>
</html>

The menu component (with routerLinks):

<nav class="navbar navbar-expand-lg navbar-light bg-light">
  <a class="navbar-brand" routerLink="/">Dev menu</a>
  <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
    <span class="navbar-toggler-icon"></span>
  </button>
  <div class="collapse navbar-collapse" id="navbarSupportedContent">
    <ul class="navbar-nav mr-auto">
      <li class="nav-item dropdown">
        <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Login</a>
        <div class="dropdown-menu" aria-labelledby="navbarDropdown">
          <a class="nav-link" routerLink="/login">Login</a>
          <a class="nav-link" routerLink="/login/C0b0ll">Login as C0b0ll</a>
        </div>
      </li>
    </ul>
  </div>
</nav>

app.component.html (with router-outlet):

<devMenu></devMenu>
<router-outlet></router-outlet>

App.module.ts:

import { BrowserModule } from "@angular/platform-browser";
import { NgModule } from "@angular/core";

// Application components.
import { AppComponent } from "./app.component";
import { AppRoutingModule } from "./app-routing.module";
import { DevMenuComponent } from "./devMenu/devMenu.component";

// Game components.
import { LoginComponent } from "./login/login.component";

@NgModule({
  imports: [
    BrowserModule,
    AppRoutingModule
  ],
  declarations: [
    AppComponent,
    DevMenuComponent,
    LoginComponent
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

app-routing.module.ts

import { NgModule } from "@angular/core";
import { RouterModule, Routes } from "@angular/router";

import { LoginComponent } from "./login/login.component";

const routes: Routes = [
  { path: "login", component: LoginComponent },
  { path: "login/:login", component: LoginComponent },

  { path: "", redirectTo: "/login", pathMatch: "full" }
];

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

1 Answers

4
votes

Depending what you are using to host your application, you need to setup URL rewrite rules.

See official docs

Apache:

RewriteEngine On
# If an existing asset or directory is requested go to it as it is
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d
RewriteRule ^ - [L]

# If the requested resource doesn't exist, use index.html
RewriteRule ^ /index.html

NGinx:

try_files $uri $uri/ /index.html;

IIS:

<system.webServer>
  <rewrite>
    <rules>
      <rule name="Angular Routes" stopProcessing="true">
        <match url=".*" />
        <conditions logicalGrouping="MatchAll">
          <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
          <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
        </conditions>
        <action type="Rewrite" url="/index.html" />
      </rule>
    </rules>
  </rewrite>
</system.webServer>

Firebase:

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