In this article, We will learn how to solve 404 file or directory not found angular error in production.
Refresh browser angular 404 file or directory not found error
You have built an Angular app and created a production build with ng build --prod You deploy it to a production server. Everything works fine until you refresh the page. The app throws The requested URL was not found on this server message (Status code 404 not found). It appears that angular routing not working on the production server when you refresh the page.
The error appears on the following scenarios
- When you type the URL directly in the address bar.
- When you refresh the page
- The error appears on all the pages except the root page.
Reason for the requested URL was not found on this server error
In a Multi-page web application, every time the application needs to display a page it has to send a request to the web server. You can do that by either typing the URL in the address bar, clicking on the Menu link/ button. Every such action results in a new request being sent to the Web server. For each request, a corresponding page must exist on the server. The server locates that page and returns it back to the client.
But in Angular Apps, the app starts when the main page (index.html) is loaded. Any subsequent requests do not reload the page but only load a part of the page. The requests are not sent to the server but handled by the Angular router at the client-side.
Now when you click on example.com/products, the request is not sent to the server but handled at the client-side by the Angular Router. The Router loads the component associated with the products route. The products component may send an HTTP request to fetch the list of products to display. But the request to display the products page is never sent to the server.
What happens when somebody types the example.com/products in the address bar or refreshes the page. Now the request to fetch the products page is sent to the server. Since there is no products page available in the server it returns the The requested URL was not found on this server or 404 error page.
Solution For 404 file or directory not found error in angular
IIS Server
You have to write following code inside web.config file to the root of the application.
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<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>
</configuration>
Angular bloggers are content creators and software developers who share knowledge, tutorials, best practices, and updates related to the Angular framework. Through blogs, articles, and technical guides, they help developers learn concepts such as components, services, dependency injection, routing, and state management. Their content ranges from beginner-friendly introductions to advanced topics, making Angular development more accessible to a wide audience. Many Angular bloggers also review new framework releases and explain how developers can adopt the latest features effectively.
ReplyDeleteThe article clearly explains a common Angular deployment issue where client-side routes work during navigation but produce a 404 error when the browser is refreshed or a route is entered directly. The key point is that Angular handles subsequent navigation through its client-side router, while a direct browser request sends the route to the production server.
ReplyDeleteThe explanation of IIS URL rewriting provides a practical solution by redirecting requests that do not correspond to an existing file or directory back to index.html. This allows the Angular router to take over and resolve the requested route correctly. These concepts are useful to explore through Angular Online Course.
The article also demonstrates why understanding the difference between server-side requests and client-side routing is important when deploying single-page applications. Developers working with Angular can use this knowledge to diagnose refresh-related routing problems and configure production environments more effectively through TypeScript Online Course.
ReplyDeleteThe IIS web.config example makes the solution particularly practical, showing how non-file and non-directory requests can be rewritten to the application's index.html. This approach helps ensure that routes such as /products continue to work correctly after deployment, making the topic relevant when developing and deploying Angular Projects.
ReplyDelete