404 error while updating page - Angular 4

Asked

Viewed 1,003 times

0

Guys is the following, I started a new project in Angular 4 and it works perfectly on the localhost. I went up to the server (Tomcat) and I was testing when searching the url http://aplicativo.com/teste he redirects me to http://aplicativo.com/teste/home (default) so far so good, but when I refresh the page (F5) he shows me the error HTTP Status 404 - /teste/home

Note: If I, with the page teste/home being displayed, go to the search field by url that contains http://aplicativo.com/teste/home and give ENTER he also shows me the same mistake HTTP Status 404 - /teste/home

How to fix this ?

1 answer

2


According to the documentation the default routes have to redirect pro index https://angular.io/guide/deployment#routed-apps-must-fallback-to-indexhtml

rewrite . 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
RewriteRule ^ /index.html

for specific apache configuration: https://ngmilk.rocks/2015/03/09/angularjs-html5-mode-or-pretty-urls-on-apache-using-htaccess/

ANOTHER SOLUTION

Instead of updating your server to serve index.html, just switch to the Hashband approach.

Import into app.module.ts:

import { HashLocationStrategy, LocationStrategy } from '@angular/common';

And add to Ngmodule:

{provide: LocationStrategy, useClass: HashLocationStrategy}
  • Thanks brother, I had not found in the documentation but with that I got !!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.