-1

I have an application on Angular 8 and after I login when in dev environment everything works just fine, the app is redirected to "/" (Here (1)) just like it should.

The problem is when I deploy it, after login the app goes to "url.com/login" and throw a 404 error. If I manually set to "url.com" everything works just fine.

This is the code on login Component:

    ngOnInit() {

    ...

       // get return url from route parameters or default to '/'
       this.returnUrl = this.route.snapshot.queryParams['returnUrl'] || '/';
       this.routeUsername = this.route.snapshot.params.username;

    }

    onSubmit() {
       this.authenticationService.login(this.f.username.value, this.f.password.value)
       .pipe(first())
       .subscribe(
          async data => {
             if (data.auth) {
                await this.commonSer.setLoginDetail(data);
                this.router.navigate(['/']); <-- Here (1)
                location.reload(); <-- Here (2)
             } else {
              
             ...

             }
          }, error => {

           ...

          });
    }

I also tried replacing "this.router.navigate(['/']);" for "this.router.navigateByUrl(this.returnUrl);", but the result was exactly the same.

Another 2 awkwards situations are:

  1. if I type any url on dev environment it works just fine, if I type a url on server, it throws a 404 error.
  2. I had to add a location.reload() (Here (2)) in order to run any url. Otherwise even if I click on menu the url on browser changes but, nothing happens (both dev and production). And that´s something I don´t see in other people codes that often. I don´t know why I need it.

What am I missing here? What makes everything behaves the way it is?

This is app-routing.module.ts

const routes: Routes = [
   { path: 'home', component: HomeComponent, canActivate: [AuthGuard] },
   ...
   //Login and Register Routes
   { path: 'login', component: LoginComponent },
   { path: 'login/:username', component: LoginComponent },
   { path: 'register', component: RegisterComponent },
   { path: 'emailConfirmation', component: EmailConfirmationComponent },
   { path: 'emailConfirmation/:username', component: EmailConfirmationComponent },
   ...

   // Root
   { path: '', component: HomeComponent, canActivate: [AuthGuard] },
   // Otherwise redirect to home
   { path: '**', redirectTo: '/' }
];

UPDATE 1: This is .htaccess

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
`enter code here`RewriteRule ^ /index.html

UPDATE2:

I also can´t use back and forward on browser. The url changes but it has an effect only if I reload page.

  • Your server is probably not set up to host Angular (or similar Frameworks). That means the problem is probably a [missing/wrong .htaccess configuration](https://stackoverflow.com/questions/22739455/htaccess-redirect-for-angular-routes) on your server. – Neyxo Feb 26 '21 at 13:51
  • You can try to add the prop `relativeTo` to your `navigate` method. For example, `this.route.navigate(['/'], {relativeTo: this.route}` or `{relativeTo: this.route.parent?}`. – Dmitry S. Feb 26 '21 at 13:56
  • I just updated @Neyxo. I tryed a few things but I never got it right. – Renato Azevedo Zoppei Feb 26 '21 at 14:21
  • I´ve tryed that @DmitryS. And I really appreciate it. But that was not the problem. – Renato Azevedo Zoppei Feb 26 '21 at 14:22

1 Answers1

0

If anyone has the same issue, This is how I fixed it.

I made a copy of the .htaccess file to /dist folder and now everything is just fine.