Incase you are using Nginx, the above .htacess method doesn't work. My context is deploying am app but with a react-vite app in a sub folder to an nginx server. Am using react router v6.20.
- You need to configure your vite app well in the vite.config file.
export default {
base: '/mydirectory',
}
- Add the same basename in your react router configuration in your main.js. Mine looks like this
import React from "react";
import ReactDOM from "react-dom/client";
import "./index.css";
import {
createBrowserRouter,
RouterProvider,
} from "react-router-dom";
import Root from "./routes/root";
import ErrorPage from "./error-page";
import About from "./AboutPage";
const router = createBrowserRouter(
[
{
path: "/",
element: <Root />,
errorElement: <ErrorPage />,
children: [
{
path: "/about",
element: <About />,
},
],
},
],
{
basename: "/mydirectory",
}
);
ReactDOM.createRoot(document.getElementById("root")).render(
<React.StrictMode>
<RouterProvider router={router} />
</React.StrictMode>
);
Make sure to replace "mydirectory" with your own.
- Create a build.
npm run build
- Go to your Ngnix file. Configure it this way in your nginx server block
location /mydirectory {
try_files $uri $uri/ /mydirectory/index.html;
}
This is what worked for me using Nginx.