React Router 404 on Refresh How to Fix SPA Routing Errors

A react router 404 on refresh error occurs when a React application works correctly while navigating between pages, but displays a “404 Not Found” error after refreshing a nested route or opening it directly. This usually happens because the web server cannot locate the requested route and does not automatically serve the React application’s index.html file.
Fixing this issue requires configuring your hosting environment to support client-side routing. Whether you deploy your React application on Apache, Nginx, cPanel, or a static hosting platform, the correct fallback configuration helps ensure that direct URLs and browser refreshes load the intended page.

What Causes a React Router 404 on Refresh?
React applications commonly use client-side routing to switch pages without requesting a completely new HTML document from the server. However, when someone refreshes a nested URL, the browser sends a request directly to the web server.
If the server does not recognize that URL as a client-side route, it may return a 404 response instead of loading the React application.
Missing Server Fallback
The server searches for a physical file or directory matching the requested route rather than serving index.html for application routes.
Incorrect Hosting Configuration
Apache, Nginx, or another hosting platform may lack rewrite rules that redirect eligible frontend requests to the React entry point.
Incorrect Base Path
An application deployed inside a subdirectory may reference JavaScript, CSS, or other assets using paths that do not match its actual hosting location.
Backend and Frontend Route Conflicts
API endpoints and frontend routes may overlap, causing incorrect fallback behavior when the server does not distinguish application pages from backend requests.
How to Fix React Router 404 on Refresh
The most common solution for a React application using BrowserRouter is to configure the web server to serve index.html when a request matches a frontend route. React Router can then interpret the URL and render the correct component.
Step 1: Check your React Router configuration. Ensure your routes are defined correctly and that the application uses BrowserRouter where appropriate. For example, a route such as /dashboard/settings should have a matching route definition or an appropriate nested route structure.
Replace Home, Dashboard, and NotFound with your actual components. This configuration handles routing inside React, but it does not fix server-side 404 errors by itself.
Step 2: Configure the server fallback. For Apache hosting, add an appropriate .htaccess file in the deployed application’s document root. For Nginx, configure the relevant server block with a try_files fallback. Ensure API routes and existing static files continue to work correctly.
Step 3: Rebuild and test the deployment. Run your production build command, upload the updated files if necessary, and test nested URLs by opening them directly in a new browser tab and refreshing. Consult the React Router documentation and your hosting provider’s configuration guidance for platform-specific details.
React Routing
1 Configure React Router
Use React Router to navigate between the home page, dashboard, and a custom 404 page without reloading the entire application.
import { BrowserRouter, Routes, Route } from "react-router-dom";
function App() {
return (
<BrowserRouter>
<Routes>
<Route path="/" element={<Home />} />
<Route path="/dashboard" element={<Dashboard />} />
<Route path="*" element={<NotFound />} />
</Routes>
</BrowserRouter>
);
}
export default App;
Fix React Router 404 on Refresh on Common Hosting Platforms
The exact fix depends on where your React application is hosted. Choose a configuration that matches your server and deployment structure rather than applying rewrite rules blindly.
Apache and cPanel
Use rewrite rules that direct eligible requests to index.html, while preserving real files and directories and excluding API endpoints where necessary.
Nginx hosting
Configure try_files to serve existing files first and use the application entry point as the fallback for frontend navigation requests.
Static hosting platforms
Configure the platform’s supported SPA rewrite or fallback mechanism so direct requests to client-side routes return the application entry point.
Six Best Practices to Prevent React SPA Routing Errors
Test direct URL access
Open every important nested route directly in a browser to verify that refreshes, bookmarks, and shared links load the intended page correctly.
Preserve backend API responses
Configure frontend fallbacks carefully so requests to API endpoints return their intended responses instead of incorrectly receiving the React application’s HTML document.
Verify asset paths
Check your build configuration and public base path when deploying to subdirectories, ensuring JavaScript bundles, CSS files, images, and fonts load correctly.
Use meaningful error pages
Create a React NotFound component for invalid application routes, while allowing genuine missing resources and backend requests to return appropriate HTTP status codes.
Automate deployment testing
Include direct-route checks in your release workflow to identify broken rewrites, missing assets, and routing regressions before users encounter them in production.
Document hosting configuration
Maintain version-controlled deployment rules and environment-specific settings so future developers can reproduce the working configuration across staging and production environments.
Frequently Asked Questions
Does BrowserRouter cause 404 errors after refresh?
BrowserRouter uses normal browser URLs, which require compatible server configuration. It works correctly when the hosting server supports client-side routing through a suitable fallback rule.
Should I use HashRouter instead of BrowserRouter?
HashRouter stores the route after a # in the URL, so the server generally receives only the part before the hash. It can avoid server fallback configuration, but produces different URLs and may not suit every application.
How can I fix React Router 404 errors on Vercel or Netlify?
Use the hosting platform’s supported SPA rewrite configuration to direct frontend route requests to your application’s entry point. Keep API routes, redirects, and other resource handling rules compatible with that fallback.