Routes Are Not Valid When Using Basename Property Of Router On Production Mode

- 1 answer

I'm using this Router:

import {BrowserRouter as Router, Route, Switch } from "react-router-dom";

<Router basename={process.env.REACT_APP_ROUTER_BASE || '/MyApp'}>
        <Route path="/" exact component={HomePage} />
        <Route path="/login" component={Login} />
        {/*<Redirect path="*" to="/" />*/}

When working with dev mode (npm start), it's working and redirecting as expected.

When working with production mode (npm build --> copy the output of build to WebContent folder on a war to be deployed on tomcat), it behaves differently:

(Note: typing the route manually in dev mode working fine)

Why it differs? How it can be solved?

What I had tried:

Reference: React routing not working while manually changing URL | React-router 4

adding devServer entry (with historyApiFallback: true) to my

export default {
resolve: {
    extensions: ['*', '.js', '.jsx', '.json'],
    // To support react-hot-loader
    alias: {
        'react-dom': '@hot-loader/react-dom'
devtool: 'source-map', // more info: and,
devServer: {
    historyApiFallback: true,


What historyApiFallback: true does is it renders index.html if the requested file doesn't exist. Your production webserver is not doing this, so when it attempts to load /MyApp/login, it see that there is no file called /MyApp/login (or /MyApp/login/index.html depending on configuration) and returns a 404.

Since React Router is handling your routing logic client side, you need to set up your webserver to fallback to delivering index.html if it can't find a page.