A React app on Tomcat shows 404 for every route except the home page
The requested resource [/myapp/dashboard] is not availableShort answer
Tomcat looks for a real file at the requested path, and a React Router route such as /dashboard is not a file, so it replies 404. Add Tomcat's rewrite valve with a rule that serves index.html for any path that is not a file, and build the app for the context path the WAR is deployed under.
Fix it fast
1Find out which cause you have
Request the home page, one deep route and the script file the home page loads. Home 200 with the deep route 404 points to cause 1; a script path that lacks the context path points to cause 2.
curl -s -o /dev/null -w "home: %{http_code}\n" http://localhost:8080/myapp/ curl -s -o /dev/null -w "deep link: %{http_code}\n" http://localhost:8080/myapp/dashboard curl -s http://localhost:8080/myapp/ | grep -o '<script[^>]*src="[^"]*"'2Match what you see, then jump to its fix
- 1
Home returns 200, /myapp/dashboard returns 404 with 'The requested resource [/myapp/dashboard] is not available'Nothing tells Tomcat to serve index.html for paths that are not filesFix - 2
The page is blank, and the script or stylesheet request goes to /assets/... without /myapp in front and returns 404The build was made for the site root, but the WAR runs under a context pathFix - 3
rewrite.config and context.xml are in the WAR, and deep links still return 404Tomcat never reads the valve declaration inside the WARFix - 4
Deep links now work, but a request to /myapp/api/users returns the HTML page instead of dataThe fallback also answers the Java backend's URLs with index.htmlFix
- 1
What does the error look like?
The requested resource [/myapp/dashboard] is not availableThe origin server did not find a current representation for the target resourceThe home page loads, but a refresh or a pasted link to any other route returns Tomcat's 404 page
Cause 1 of 4
Nothing tells Tomcat to serve index.html for paths that are not files
1Check
A route like /dashboard exists only inside your JavaScript. Tomcat maps the URL to a file in the web application, finds none and answers 404. See whether the WAR carries a rewrite configuration at all.
unzip -l myapp.war | grep -E 'WEB-INF/rewrite.config|META-INF/context.xml'2Fix
Add the rewrite valve to the web application and give it one rule. The valve reads WEB-INF/rewrite.config, and a valve declared in the application's own context.xml matches against the path without the context path. The -f test looks the name up among the application's files, so real files (scripts, styles, images) are still served as they are. Put both files into your build output so they land inside the WAR.
<!-- META-INF/context.xml -->
<Context>
<Valve className="org.apache.catalina.valves.rewrite.RewriteValve" />
</Context>
# WEB-INF/rewrite.config
RewriteCond %{REQUEST_PATH} !-f
RewriteRule ^/(.*)$ /index.html [L]3Verify
Redeploy the WAR, then request the deep link. It should return 200 and your index.html.
curl -s -o /dev/null -w "%{http_code}\n" http://localhost:8080/myapp/dashboardConfirmed by tomcat.apache.org
Cause 2 of 4
The build was made for the site root, but the WAR runs under a context path
1Check
Tomcat takes the context path from the WAR's file name: myapp.war runs at /myapp, and only ROOT.war runs at the root. If the built index.html points at /assets/..., the browser asks the root application for them. Print the URLs the build wrote.
grep -oE '(src|href)="[^"]*"' dist/index.html2Fix
Build with a base path equal to the context path, and tell the router the same prefix. The other way out is to deploy the WAR as ROOT.war and keep the base at /.
npx vite build --base=/myapp/
// main.jsx
<BrowserRouter basename="/myapp">
<App />
</BrowserRouter>3Verify
The built index.html now lists /myapp/assets/... URLs, and one of them returns 200.
grep -o 'src="[^"]*"' dist/index.html
curl -s -o /dev/null -w "%{http_code}\n" http://localhost:8080/myapp/Confirmed by vite.dev
Cause 3 of 4
Tomcat never reads the valve declaration inside the WAR
1Check
The context.xml inside the application is only parsed when the Host allows it. deployXML defaults to true, but defaults to false when a security manager is enabled. A file in the host's configuration folder also wins over the packaged one.
grep -n "deployXML" "$CATALINA_BASE/conf/server.xml"2Fix
Set deployXML to true on the Host, or move the Context definition into $CATALINA_BASE/conf/[engine name]/[host name]/myapp.xml, which takes precedence over the packaged file. The Tomcat docs advise security-conscious setups to keep deployXML false, so the second route is the safer one there. Restart Tomcat after either change.
<Host name="localhost" appBase="webapps" deployXML="true">3Verify
After the restart, request the deep link again and expect 200.
curl -s -o /dev/null -w "%{http_code}\n" http://localhost:8080/myapp/dashboardConfirmed by tomcat.apache.org
Cause 4 of 4
The fallback also answers the Java backend's URLs with index.html
1Check
This applies when the same WAR also holds servlets or a Spring controller. A servlet URL is not a file, so the first rule rewrites it too. Look at the start of the response body.
curl -s -i http://localhost:8080/myapp/api/users | head -n 122Fix
Exclude the API prefix with a second condition. Several RewriteCond lines before one rule must all be true, and a leading ! negates a pattern. The path in the condition has no context path in it.
# WEB-INF/rewrite.config
RewriteCond %{REQUEST_PATH} !^/api/
RewriteCond %{REQUEST_PATH} !-f
RewriteRule ^/(.*)$ /index.html [L]3Verify
The API URL now returns its real status and a JSON content type, while /myapp/dashboard still returns the index page.
curl -s -o /dev/null -w "%{http_code} %{content_type}\n" http://localhost:8080/myapp/api/users
curl -s -o /dev/null -w "%{http_code}\n" http://localhost:8080/myapp/dashboardConfirmed by tomcat.apache.org
How do I stop it coming back?
- Decide the URL first: name the WAR after the context path you want, or use ROOT.war, and build the front end with the same base.
- Keep rewrite.config and context.xml in the front-end repository, in a folder your build copies into the WAR, so no release can forget them.
- Keep every backend URL under one prefix such as /api, so the exclusion in the rule stays one line.
- After each deploy, run the three requests from the first step: home, a deep link and a script file.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- TaskvareA task and project-management suite: React on the web, Flutter on mobile, TypeScript underneath.
Related fixes
Web apps
A Vite single page app shows 404 when you refresh a route on the live site
Page not found (Netlify) on a route that works inside the app
Client side routes like /dashboard are not real files in dist.
3 causes, each with a fixWeb apps
Vite production build loads a blank page with 404s on its files
Blank page after deploy; the console lists failed requests for /assets/...
Vite writes asset URLs that start at the site root unless told otherwise.
3 causes, each with a fixWeb apps
A React app on Netlify shows 404 when you reload a route
Opening /about from a link works; pressing refresh or pasting the URL gives 404
Netlify looks for a real file at the URL you reload, finds none for a client-side route like /dashboard, and returns 404.
3 causes, each with a fixWeb apps
Module not found for antd/dist/antd.less after upgrading to Ant Design 5
Module not found: Error: Can't resolve 'antd/dist/antd.less'
Ant Design 5 dropped Less and the bundled CSS file for CSS-in-JS, so antd/dist/antd.less no longer exists.
3 causes, each with a fixWeb apps
Astro Code component fails inside a React component
Astro components cannot be rendered directly via function call
Astro components such as Code only exist on the server and render static HTML.
2 causes, each with a fixWeb apps
React shows Attempted import error when importing a component or function
Attempted import error:
The build found the file but not the name you asked for.
2 causes, each with a fix