0

I have route

import { HashRouter } from 'react-router-dom';

 <HashRouter>
  <BrowserRouter>
    <Switch>
      <Route path="/admin" render={props => <AdminLayout {...props} />} />
      <Route path="/auth" render={props => <AuthLayout {...props} />} />
      <Redirect from="/" to="/auth/login" />
    </Switch>
  </BrowserRouter>
  </HashRouter>

When I refresh the page on the server getting a 404 error. also how to set base URL?

3
  • That doesn't sound like a react or react-router problem, it sounds like a webserver configuration problem, or the use of a relative script in your index.html (./reactprojectname.js instead of a absolute path /reactprojectname.js) Commented Jun 4, 2020 at 17:07
  • this might help you stackoverflow.com/questions/27928372/… Commented Jun 4, 2020 at 17:10
  • You can check this about how to solve 404 Error on servers. I am also wondering if we use both HashRouter and BrowserRouter together in an app? Here is example of How to use HashRouter with basename. Commented Jun 4, 2020 at 17:24

1 Answer 1

2

This is really simple.

Create a .htaccess file on /public_html folder and copy that code:

RewriteBase /
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.html [L]

If you want to know about the process, read this article: https://ui.dev/react-router-cannot-get-url-refresh/

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

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

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.