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>
<Route exact path="/">
...
</Route>
</BrowserRouter>
Then, in package.json
, add:
homepage: "https://example.com/mySubdirectory"
(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();
app.use(historyFallback);
Without this, users will not be able to directly enter https://example.com/mySubdirectory/my_route
in the browser's url bar; they'll get a cannot GET
error.
You can see an example of the above with Apache here.