refreshing pages gives 404 error for create-react-app websites using react-router-dom

杀马特。学长 韩版系。学妹 提交于 2020-06-23 03:54:27

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!