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.
- The error occurs only if you are using HTML 5 Routing or PathLocationStrategy
This is due to the fact that the Angular is a SPA (Single Page Application) framework.
Table of Contents
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 webserver. 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. You can read more about it from the article Angular bootstrap process
For Example, when you request for example.com
the request is sent to the server, which returns the index.html
.This will bootstrap the Angular.
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
Workaround
The workaround to this problem is to instruct the server to load the index.html
no matter what. So when the request comes to example.com/products
page, the server serves the index.html
page. This bootstraps the Angular app and the Angular router is smart enough to read the requested URL and load the products
component.
To instruct the server to load the index.html
for every request require a configuration change at the Web server level. The configuration settings depend on the server used.
IIS Server
First, install the IIS URL Rewrite module. Copy the following to web.config file
to the root of the application.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | <?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> |
Apache
Add a rewrite rule to the .htaccess
file as shown.
1 2 3 4 5 6 7 8 9 10 | 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 |
Nginx
Add the following try_files
rules to the Nginx configuration file to redirect every request to serve index.html
1 2 3 | try_files $uri $uri/ /index.html; |
References
Routed apps must fallback to index.html
Angular Bootstrap Process
Location strategies in Angular
Angular Router
which verser will be best for iis server, 3.5 or 4.5 .net server?
Thank you soo much for your suggestion, your htaccess code work for me
Thank you. It works on my Angular14
It is working fine. Thank you !
thnx it work for iis
Thanks, really helpful
For Apache works for me..thank you
omg! thx a lot!!!!
It worked as a charm!
Thank you =]
Use this to set a handler for any URL that doesn’t map to anything in your filesystem.
FallbackResource /my-app/index.html
Add this line of code in your httpd.conf or in /etc/apache2/sites-enabled/000-default.conf configuration file.
Your angular application routing will work on refresh.
I am connecting IDP server for authentication i am sending client id and redirect url from angular application, server is replaying with the code now i am getting 404 error (in redirect url i have give domainname/routename ) how we can solve this issue
Just adding–in case if you deployed in any other folder, you need to add that folder name as ; RewriteRule ^ /