Hosting React and Angular Apps
Single-page apps are just static files — but their client-side routes need a rewrite rule so refreshing a page does not return 404.
On this page · 5 min · Intermediate
On this page
- In simple words
- Learning objectives
- Explanation
- Code example
- Step by step
- Real-world use
- Common mistakes
- Best practices
- Remember this
- Practice exercise
- Mini quiz
- Related topics
- My notes
- Reading time
- 5 min
- Difficulty
- Intermediate
- Updated
- Oct 30, 2026
Related tutorials
In simple words#
A React app is one big book (index.html) with many chapters. When someone asks IIS for “/dashboard”, IIS looks for a room called dashboard and finds nothing. The rewrite rule says: “for any unknown address, hand over the book — it will open the right chapter itself.”
- SPA
- Single-page app: one HTML file, routes handled in the browser.
- URL Rewrite
- IIS module that rewrites unknown URLs to index.html.
- Base href
- The path the app is served from (/ or /app/).
Learning objectives#
- Deploy a React or Angular build to IIS
- Write the SPA fallback rewrite rule
- Exclude API paths
- Cache files correctly
Explanation#
Build the app: npm run build (React/Vite → dist; Angular → dist/<app>/browser). Copy the output to the site folder.
Install the URL Rewrite module. Add a rule that rewrites requests which are not real files or folders to /index.html.
Exclude /api (and other apps) from the rule so backend calls are not rewritten.
Hashed asset files can be cached for a year; index.html should not be cached so new releases appear immediately.
Code example: web.config for a React or Angular SPA#
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="SPA fallback" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{REQUEST_URI}" pattern="^/api/" negate="true" />
</conditions>
<action type="Rewrite" url="/index.html" />
</rule>
</rules>
</rewrite>
<staticContent>
<clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="365.00:00:00" />
</staticContent>
</system.webServer>
<location path="index.html">
<system.webServer>
<staticContent><clientCache cacheControlMode="DisableCache" /></staticContent>
</system.webServer>
</location>
</configuration>
Refreshing https://shop.example.com/products/5 loads the app correctly; /api calls reach the API; assets are cached for a year, index.html never.
This example is illustrative. Copy or download it and run it in your own environment; the expected output is shown above.
Step-by-step explanation#
- The rule matches every URL.
- Conditions skip real files, real folders and /api.
- Everything else is rewritten (not redirected) to index.html.
- Cache headers differ for hashed assets and index.html.
Real-world use case#
An Angular admin portal and its ASP.NET Core API run on the same IIS site, with the API as a child application at /api.
Common mistakes#
The URL Rewrite module is not installed. Install it, then reload.
API calls get HTML back and JSON parsing fails. Exclude API paths.
Best practices#
- Install URL Rewrite before deploying
- Exclude backend paths
- No-cache for index.html
- Copy the whole build folder each release
Remember this#
SPA on IIS = static files + rewrite to index.html.
Skip real files, folders and /api.
Rewrite ≠ redirect: the URL stays the same.
Practice exercise#
Your task: An Angular app is hosted under /admin/. What else must match?
Show solution
Build with --base-href /admin/ and rewrite unknown URLs to /admin/index.html (place the web.config in the /admin folder).
Mini quiz#
Why does refreshing /dashboard in a SPA return 404 without a rewrite rule?
Want more? Practise all IIS MCQs · IIS interview questions
My notes#
Saved in this browser.