0

I want to add the base URL to my react.js project. However I had tried couple of methods that did not work.

For example, if my project url is :

http://www.myproject.com/ 

It should appear in address bar as :

http://www.myproject.com/app
http://www.myproject.com/app/home  // If home page 
http://www.myproject.com/app/about // If about page 

Below is my project code.

index.js

import "./index.css"
import React from 'react';
import ReactDOM from 'react-dom/client';
import { AuthProvider } from './context/AuthProvider';
import {BrowserRouter, Routes, Route} from 'react-router-dom'
import { disableReactDevTools } from '@fvilers/disable-react-devtools';
import App from './App';

if (process.env.NODE_ENV === 'production') {
  disableReactDevTools();
}

const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
    <BrowserRouter>
      <AuthProvider>
        <Routes>
          <Route path="/*" element={<App />} />
        </Routes>
      </AuthProvider>
    </BrowserRouter>
);

App.js

import Layout from './components/authController/Layout'
import Login from './components/authController/Login';
import Home from './components/authController/Home'
import AllUser from './components/authController/AllUser'
import RequireAuth from './components/authController/RequireAuth';
import PersistLogin from './components/authController/PersistLogin';
import Logout from './components/authController/Logout';
import Missing from './Missing';
import {Routes, Route, Navigate} from 'react-router-dom'


function App() {
  return (
    <div>
    <Navbar/>
    <Routes>
      <Route path="/" element={<Layout />}>
          {/* public routes */}
          <Route path="login" element={<Login />} />
          <Route path="loggedOut" element={<Logout />} />
          
        {/* we want to protect these routes */}
        <Route element={<PersistLogin />}>
          <Route path="/" element={<Navigate to="/home" replace />} />
            <Route path="home" element={<Home/>} />

            <Route element={<RequireAuth/>}>
              <Route path="alluser" element={<AllUser />} />
            </Route>

        </Route>
          {/* catch all */}
          <Route path="*" element={<Missing />} />
      </Route>
  </Routes>
  </div>
  );
}

export default App;

What I had tried?

As I referred in stackoverflow and also many other websites, integrated below methods.

  1. Added base url inside package.json file
    "homepage": "/app",
  2. Tried to pass the tag <base href="http://myapp.com/app"> within <head> tag in public/index.html page
  3. Passed the base url within BrowserRouter in index.js <BrowserRouter basename="app"> </BrowserRouter>

I am sure I am missing something, it would be great learning if someone help on this or give insights on this.

Thank you

2
  • Is your react app actually deployed to and served from a "/app" directory on the server? The basename prop is a way to prefix routes/links automatically to match the directory where the app is served from. Commented Aug 1, 2022 at 18:01
  • 1
    i think the best way is to redirect users from / to /app Commented Aug 2, 2022 at 1:16

2 Answers 2

2

I don't know the correct answer to this , but I believe that you can redirect the route "/" to "/app" . I hope this can help you

Sign up to request clarification or add additional context in comments.

Comments

0

Solution:-

Hello everyone.

I got the solution for my query. I have modified the index.js file like below

 <Routes>
     <Route path="app/*" element={<App />} />
     <Route path="/*" element={<Navigate to="app/home" replace />} />
 </Routes>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.