answers to this and similar questions seem to neglect that, if using client-side routing (like react router) there are some back-end changes that should be made when serving from a subdirectory.

there are several approaches to this. one is to use <hashrouter>; that approach is described well here, or in the react router docs.

another approach is to use <browserrouter> with express. here are the steps for that:

in your app:

    <browserrouter basename={process.env.public_url}>
        <route path="/your_route">
        <route exact path="/">

then, in package.json, add:

homepage: ""

(note that "." will not work, if you're using client-side routing.)

then, in express, try the connect-history-api-fallback package (which is recommended in the vue router docs).

const express = require('express');
const historyfallback = require('connect-history-api-fallback');

const app = express();

without this, users will not be able to directly enter in the browser's url bar; they'll get a cannot get error.

you can see an example of the above with apache here.


in package.json

"homepage": ".",


if you are using react-router-dom, if you know your directory name you could set basename to "/directory-name" in the router


if you want to set the base name to be dynamic, use

(note: this won't be useful if you are using sub routing)


incase of sub routing, set a standard string to your routes


 let basename_path = null;
   var url = window.location.pathname.tolowercase();
   if(url.indexof("react") === -1){    // if string(react) not available
            basename_path = "/";
       var array = url.split("react");
       basename_path = array[0]+"react/";

 <router basename={basename_path}>   
   <route exact path="/react/home" component={home}/>
   <route exact path="/react/contactus" component={contactus}/>
   <route exact path="/react/aboutus" component={aboutus}/>               

Related Query

More Query from same tag