SPA Redirects on Netlify Without a _redirects File
Created by brian on
When deploying a single-page application to Netlify, you need a catch-all redirect so that client-side routing works on direct navigation and page refreshes. The typical approach is to add a _redirects file to your public directory:
txt/* /index.html 200
But there's a simpler option -- generate it at build time by chaining an echo onto your build command in the Netlify UI:
txtnpm run build && echo "/* /index.html 200" > dist/_redirects
This appends a step after your build that writes the _redirects file directly into the output directory. No extra file to maintain in your repo, no Netlify plugin needed, and no netlify.toml required.
Why this works
Netlify runs whatever you put in the Build command field as a shell command. By chaining with &&, the redirect file only gets created if the build succeeds. Netlify then picks up _redirects from the publish directory (dist/) automatically.
Adjust for your setup
If your framework outputs to a different directory, swap dist/ accordingly:
- Vite / Astro:
dist/ - Next.js (static export):
out/ - Create React App:
build/
That's it. One line, zero config files.
Last updated