Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 2 Hosted on IIS: HTTP Error 404

I have simple app with one component that expects certain parameters from url. there is only one route in the application:

const appRoutes: Routes =                         path: 'hero/:userId/:languageId',component: HeroWidgetComponent }]; 

In the Index.html, I have this in the header <base href="/">

I am using webpack and the application runs fine on development environment, when browsing the url: http://localhost:4000/hero/1/1.

However, when building the app for production and getting the distribution files, then hosting that on IIS. I get the following Error when trying to browse the same url:

HTTP Error 404.0 - Not Found The resource you are looking for has been removed, had its name changed, or is temporarily unavailable. 

The application works fine if I remove all the routing and just browse: http:localhost:4200 on the IIS.

like image 704
Hussein Salman Avatar asked May 04 '17 14:05

Hussein Salman


People also ask

How do I fix 404 error in IIS?

Resolution. To resolve this problem, verify that the file requested in the browser's URL exists on the IIS computer and that it is in the correct location. Use the IIS Microsoft Management Console (MMC) snap-in to determine where the file requested must exist in the IIS computer's file system.

How does angular handle 404?

To set up a 404 page in the angular routing, we have to first create a component to display whenever a 404 error occurred. In the following approach, we will create a simple angular component called PagenotfoundComponent. Creating Component: Run the below command to create pagenotfound component.

Can we host angular app in IIS?

Enable IIS & Install URL Rewrite. Configure Angular to output files in the development build. Add a new application in IIS. IIS permissions.

What is 404 File or directory not found?

A 404 error is an HTTP status code that means that the page you were trying to reach on a website couldn't be found on their server. To be clear, the error indicates that while the server itself is reachable, the specific page showing the error is not.


1 Answers

We have to make IIS fall back to index.html by adding a rewrite rule.

Step 1: Install IIS URL Rewrite Module

Step 2: Add a rewrite rule to web.config

   <?xml version="1.0" encoding="utf-8"?>     <configuration>       <system.webServer>         <rewrite>           <rules>             <rule name="AngularJS 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="/" />             </rule>           </rules>         </rewrite>       </system.webServer>     </configuration> 
like image 176
Hussein Salman Avatar answered Sep 23 '22 09:09

Hussein Salman