0

whenever I enter the URL http://domain.de/beta/ to my browser, I can see the content of Verein. If I press F5 to refresh the page, I get an 404 Not-Found error.

I'm using the BrowserRouter from react-router-dom (v.5.2.0 > I checked the docs too and just follow the example).

// inside my index.js of Create-React-App render( <React.StrictMode> <BrowserRouter basename="/beta"> <Switch> <Route path="/verein/contact-imprint-privacy" component={ContactImprintPrivacy} /> <Route path="/verein" component={Verein} /> <Route exact path="/"> <Redirect to="/verein" /> </Route> <Route path="*" component={FourOhFour} /> </Switch> </BrowserRouter> </React.StrictMode>, document.getElementById("root") ); 

I run yarn build and deploy the web app from build folder to my hosting space. What do I miss for the page refresh?

Update, 11.07:

I have to think about how the server side should handle the URL. So, I add an .htaccess file with a rewrite rule.

<IfModule mod_rewrite.c> RewriteEngine On RewriteBase /beta RewriteRule ^index\.html$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-l RewriteRule . /beta/index.html [L] </IfModule> 

1 Answer 1

1

When you refresh your application, you get a 404 Not-Found error, because your application go to the server to find /verein page, but there is none. React router is just client side routing, but the very first request will always be to the server.

You need to setup your server to catch all incoming url (/*) and render your index.html

for more detailed answer, you can read this questions accepted answer:

QA

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

1 Comment

Thanks for your hint about "think about the server side". I solve my problem by using a .htaccess rewrite rule.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.