Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Router Gives 404 error on production

React Router works on local machine, Chrome but not on safari and other browsers,

 <Switch>
    <Route exact path={"/"} component={Home} />
    <Route path="/programs/:slug" component={Programs} />
    <Route path="/page/:slug" component={Page} />
    <Route component={NotFound} />
</Switch>

thats my code but it works perfectly in development mode no errors but on production, safari gives 404 error when i navigate to programs/:slug or page/:slug

like image 698
Schnecke Avatar asked Jan 27 '23 23:01

Schnecke


2 Answers

I added this to .htaccess

RewriteBase /
RewriteCond %{REQUEST_URI} !^/(assets/?|$)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.html [L]

Thanks to Kishan Mundha explanation

like image 155
Schnecke Avatar answered Feb 08 '23 01:02

Schnecke


You may need to configure single entry point index.html on server. All url should target to index.html and entry point will decide how to render and content based on route.

like image 37
Kishan Mundha Avatar answered Feb 08 '23 02:02

Kishan Mundha