Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

A React app on Tomcat shows 404 for every route except the home page

ErrorThe requested resource [/myapp/dashboard] is not available

Short 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.

  • Applies to Tomcat 10.1, React 19, React Router 6, Vite 7
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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="[^"]*"'
  2. 2Match what you see, then jump to its fix

    1. 1Home 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. 2The 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. 3rewrite.config and context.xml are in the WAR, and deep links still return 404Tomcat never reads the valve declaration inside the WARFix
    4. 4Deep 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

What does the error look like?

  • The requested resource [/myapp/dashboard] is not available
  • The origin server did not find a current representation for the target resource
  • The 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/dashboard

Confirmed by tomcat.apache.org

Did this fix it?

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.html

2Fix

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

Did this fix it?

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/dashboard

Confirmed by tomcat.apache.org

Did this fix it?

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 12

2Fix

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/dashboard

Confirmed by tomcat.apache.org

Did this fix it?

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:

Related fixes

Web 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 fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.