diff --git a/packages/vite/src/node/server/middlewares/htmlFallback.ts b/packages/vite/src/node/server/middlewares/htmlFallback.ts index b61b44bf061180..1a57bd84dc1d39 100644 --- a/packages/vite/src/node/server/middlewares/htmlFallback.ts +++ b/packages/vite/src/node/server/middlewares/htmlFallback.ts @@ -63,8 +63,20 @@ export function htmlFallbackMiddleware( } if (spaFallback) { - debug?.(`Rewriting ${req.method} ${req.url} to /index.html`) - req.url = '/index.html' + // remove a path component at a time from pathname and check for + // the presence of a corresponding index.html + let workingPathname = pathname + while (true) { + const filePath = path.join(root, workingPathname, 'index.html') + if (fs.existsSync(filePath)) { + const newUrl = workingPathname + '/index.html' + debug?.(`Rewriting ${req.method} ${req.url} to ${newUrl}`) + req.url = newUrl + return next() + } + if (workingPathname === '/') break + workingPathname = path.dirname(workingPathname) + } } next()