i would like to deploy my app in Angular to production server but I have problems. App works corectly when I use only angular routing (change component, not redirecting) but when I refresh the page in browser, I get a 404 page returned from IIS (I use IIS as the web server)
Here is my angular routing:
const appRoutes: Routes = [
{ path: '', redirectTo: '/home', pathMatch: 'full', canActivate: [AuthGuard] },
{ path: 'home', component: DashboardComponent, canActivate: [AuthGuard] },
{ path: "profile", component: UserProfileComponent, canActivate: [AuthGuard] },
{ path: '400', component: ErrorComponent },
{ path: '401', component: ErrorComponent },
{ path: '403', component: ErrorComponent },
{ path: '404', component: ErrorComponent },
{ path: '500', component: ErrorComponent },
{ path: '503', component: ErrorComponent },
{ path: '**', redirectTo: '/404' }
]
If you're using PathLocationStrategy , you may provide a APP_BASE_HREF or add a <base href> element to the document to override the default. For instance, if you provide an APP_BASE_HREF of '/my/app/' and call location.go('/foo') , the browser's URL will become example.com/my/app/foo .
HashLocationStrategylink. A LocationStrategy used to configure the Location service to represent its state in the hash fragment of the browser's URL.
The HTTP error 404, more commonly called “404 error”, means that the page you are trying to open could not be found on the server. This is a client-side incident which means either the page has been deleted or moved, and the URL has not been modified accordingly, or that you have misspelled the URL.
This work for angular 8, add .htaccess to your project file
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</IfModule>
RewriteEngine On
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.html [L]
If you are using angular 6,7 this method works (If you are okay with /#/ in your URL.
In app.module.ts
import {LocationStrategy, HashLocationStrategy} from '@angular/common';
After import add following line to providers.
{provide: LocationStrategy, useClass: HashLocationStrategy}
ex:
providers: [AuthService,
AuthGuard,
FlxUiDataTable,
{provide: LocationStrategy, useClass: HashLocationStrategy}]
This will solve your issue. Read Documentation here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With