React App-刷新页面后找不到URL

时间:2020-05-27 09:20:55

标签: reactjs react-router

我已经使用create-react-app创建了Web应用程序。一切正常-我运行npm run build并将build文件夹放在服务器上。页面有效,但是在我转到任何链接并刷新页面后,我得到了在此服务器上找不到请求的URL / nieruchomosci/。

我使用以下代码访问.htaccess:

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

package.json:

{
  "name": "sunstreetinvestment",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@testing-library/jest-dom": "^4.2.4",
    "@testing-library/react": "^9.5.0",
    "@testing-library/user-event": "^7.2.1",
    "browser-image-compression": "^1.0.9",
    "firebase": "^7.13.1",
    "materialize-css": "^1.0.0",
    "moment": "^2.24.0",
    "multiselect-react-dropdown": "^1.4.7",
    "react": "^16.13.1",
    "react-dom": "^16.13.1",
    "react-multi-select-component": "^1.3.3",
    "react-redux": "^7.2.0",
    "react-redux-firebase": "^3.3.0",
    "react-router": "^5.1.2",
    "react-router-dom": "^5.1.2",
    "react-scripts": "3.4.1",
    "react-slideshow-image": "^1.4.0",
    "redux": "^4.0.5",
    "redux-firestore": "^0.13.0",
    "redux-thunk": "^2.3.0",
    "styled-components": "^5.0.1"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": "react-app"
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  }
}

(我尝试使用“ homepage”:“。”)

,我使用BrowserRouter。

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

只需将所有404请求指向index.html

index.html将在前端正确转到URL,而不是使用服务器进行路由

例如,在您的服务器中未配置example.com/hello/world,它将为404。只需将其重定向到index.html即可加载react-router逻辑。而且react-router可以正确识别URL并呈现正确的页面