|
Getting your Trinity Audio player ready... |
If your React app works fine during navigation but shows a 404 error on page refresh after deployment on Netlify, this is not a bug — it’s a routing mismatch between React and Netlify.
This guide explains the problem deeply, provides proper fixes, tables, and FAQs to make it fully developer-friendly.
Why Does React App Show 404 Error on Refresh?
React apps usually use client-side routing (via react-router-dom), meaning:
- Routes like
/about,/dashboard,/profile - Do NOT exist as real files
- They are handled entirely inside the browser
When you navigate inside the app, React Router works perfectly.
But when you refresh a page or open a route directly, Netlify’s server receives a request like:
/about
Netlify thinks:
“User is requesting a file named
/about”
Since that file does not exist → 404 Error
React Router vs Netlify Server (Comparison)
| Feature | React Router | Netlify Server |
|---|---|---|
| Routing Type | Client-side | Server-side |
| Route Exists as File? | ❌ No | ✅ Expected |
| Handles Refresh | ❌ No | ✅ Expects file |
| Entry Point | index.html | Physical files |
👉 This mismatch is the root cause of the 404 error.
Solution: Netlify Rewrite Rule
To fix this, you need to tell Netlify:
“No matter which URL is requested, always serve
index.htmlso React Router can handle the route.”
This is done using a rewrite rule.
Method 1: Using _redirects File (Recommended)
Steps:
- Go to your React project
- Open the
publicfolder - Create a file named
_redirects - Add the following line:
/* /index.html 200

What This Rule Means
| Part | Meaning |
|---|---|
/* | Match all routes |
/index.html | Serve React entry file |
200 | Rewrite (not redirect) |
✅ Ensures React Router handles all routes after page load.
Method 2: Using netlify.toml (Alternative)
Create netlify.toml in the project root:
[[redirects]]
from = "/*"
to = "/index.html"
status = 200
⚠️ Use only one method, not both.
File Placement Rules (Common Mistakes)
| File | Correct Location |
|---|---|
_redirects | public/_redirects |
netlify.toml | Project root |
❌ _redirects inside src/ → won’t work
❌ Wrong filename → fix fails
After Adding the Fix
- Commit changes
- Push to GitHub
- Trigger a new Netlify deployment
- Clear browser cache
- Test by refreshing deep routes
Alternative Approach (HashRouter)
import { HashRouter } from "react-router-dom";
- URL becomes:
example.com/#/about
| Pros | Cons |
|---|---|
| No server config needed | ❌ SEO-unfriendly |
| No 404 error | ❌ Ugly URLs |
| Quick fix | ❌ Not professional for production |
Recommended only for quick testing, not for production apps.
Official Reference (Netlify)
For official documentation on SPA routing and redirects, visit 👉 Netlify
FAQs
Why does this issue not appear in localhost?
Local dev servers (CRA, Vite, Webpack) automatically fallback to index.html. Netlify does not do this unless configured.
Does this fix work with Vite, CRA, Next.js?
- Vite – ✅ Yes
- CRA – ✅ Yes
- Next.js – ❌ Different setup (uses SSR)
Can I use both _redirects and netlify.toml?
❌ No. Use only one to avoid conflicts.
Where should _redirects be placed?
Inside public/ folder, so it’s included in the build.
What happens if I don’t configure this?
- Deep links and page refresh will show 404
- React app works only on root
/
Final Thoughts
This is not a React bug, nor a Netlify issue —
it’s simply a routing responsibility mismatch.
Once you understand how SPA routing works, applying the Netlify rewrite fix becomes straightforward and permanent.

Arsalan Malik is a passionate Software Engineer and the Founder of Makemychance.com. A proud CDAC-qualified developer, Arsalan specializes in full-stack web development, with expertise in technologies like Node.js, PHP, WordPress, React, and modern CSS frameworks.
He actively shares his knowledge and insights with the developer community on platforms like Dev.to and engages with professionals worldwide through LinkedIn.
Arsalan believes in building real-world projects that not only solve problems but also educate and empower users. His mission is to make technology simple, accessible, and impactful for everyone.
Join us on dev community

