问题
So I've read similar questions but answers were too complex for my understanding. I have not set up a back-end, I just use create-react-app and install react-router-dom, and while my websites can refresh on my localhost, when I upload them on internet, if I refresh any page except the home page, they give a 404 not found page error.
these are the versions of my installed programs: "react": "^16.6.3", "react-dom": "^16.6.3", "react-router-dom": "^4.3.1", "react-scripts": "2.1.1"
<BrowserRouter>
<div>
<Switch>
<Route exact path="/" component={Home}/>
<Route path="/didyoukno" component={Didyouknow}/>
<Route
render={() => (
<div>
<h1>Error 404: Not Found</h1>
<Link to="/">Go Home</Link>
</div>)}
/>
</Switch>
</div>
</BrowserRouter>
回答1:
So turned out to deploy on a c-panel you need to add a .htaccess in the public folder with this:
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.html [QSA,L]
Now for refreshing or history, it will refer to index.html
来源:https://stackoverflow.com/questions/53895181/refreshing-pages-gives-404-error-for-create-react-app-websites-using-react-route