Lovable or Bolt app returns 404 on refresh: the SPA fallback for every host

By AI App Medic · Updated · Deploy · React · Lovable · Bolt

Your app works when you click around, but refreshing /dashboard or opening a shared link shows a 404 page from your host. This is not a bug in your code. Lovable, Bolt and most Vite apps are single-page applications (SPAs): there is one real file, index.html, and React Router decides what to show in the browser. The server needs one rule: if the requested file does not exist, serve index.html.

If your app is hosted on Lovable's own hosting, this is handled for you. You need the rule when you export to another host.

Netlify

Create public/_redirects (Vite copies public/ into dist/):

/*    /index.html   200

Netlify serves existing files first, so assets are not affected. The equivalent in netlify.toml:

[[redirects]]
  from = "/*"
  to = "/index.html"
  status = 200

Vercel

Create vercel.json in the project root:

{
  "rewrites": [{ "source": "/(.*)", "destination": "/index.html" }]
}

Vercel checks the filesystem before applying rewrites, so files in dist still load.

nginx (VPS)

server {
    root /var/www/myapp/dist;
    index index.html;

    location / {
        try_files $uri $uri/ /index.html;
    }
}

Run nginx -t before reloading. A syntax error stops every site on the server from reloading.

Apache / shared hosting

Add a .htaccess file next to index.html:

FallbackResource /index.html

This requires mod_dir (enabled on most hosts). The older mod_rewrite version works too:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ /index.html [L]

Cloudflare Pages

If your build output has no top-level 404.html, Cloudflare Pages treats the project as an SPA and serves index.html for unknown paths automatically. If you added a 404.html, remove it or add an explicit rule in _redirects.

Don't forget a real Not Found page

With the fallback in place, the server answers 200 for every URL, including typos. Add a catch-all route (path="*") in React Router that renders a helpful Not Found page, so users are not stuck on a blank screen and search engines do not index junk URLs.

If the page is blank even on the home route, it is a different problem. See blank page after deploy.

FAQ

Why does refreshing a page give 404 in a React app?

React Router handles routes in the browser. On refresh the browser asks the server for /dashboard, which is not a real file, so the server returns 404 unless it is told to serve index.html instead.

Will the rewrite break my JavaScript and image files?

Not with the configs below. They serve real files first and only fall back to index.html for paths that do not exist.

Should missing pages still return 404?

Ideally yes, for SEO. With an SPA fallback the server returns 200 for every path, so add a catch-all route in React Router that shows a Not Found page and sets noindex.

Related guides