.htaccess 如何正确部署react应用程序到子目录?

cgyqldqp  于 2022-11-16  发布在  React
关注(0)|答案(1)|浏览(162)

我按照在线教程将我的react应用程序部署到了一个子目录,但是当我专门搜索那个子目录(https://uuu.vip/boss)或者刷新子目录时,它只是将我链接回主页。有人知道如何修复这个问题吗?谢谢。
下面是package.json文件的代码:

{
  "name": "UUu",
  "version": "0.1.0",
  "private": true,
  "homepage": "https://uuu.vip/boss/",
  "dependencies": {
    "@material-ui/core": "^4.12.4",
    "@material-ui/icons": "^4.11.3",
    "@testing-library/jest-dom": "^5.16.4",
    "@testing-library/react": "^13.3.0",
    "@testing-library/user-event": "^13.5.0",
    "node-sass": "^7.0.1",
    "react": "^18.1.0",
    "react-dom": "^18.1.0",
    "react-router-dom": "^6.3.0",
    "react-scripts": "^5.0.1",
    "web-vitals": "^2.1.4"
  },
  "scripts": {
    "predeploy": "npm run build",
    "deploy": "gh-pages -d build",
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": [
      "react-app",
      "react-app/jest"
    ]
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  },
  "devDependencies": {
    "gh": "^2.8.9",
    "gh-pages": "^4.0.0"
  }
}

以下是App.js文件的代码:

import "./App.scss";
import Navbar from "./components/Navbar";
import ScrollToTop from "./components/ScrollToTop"
import Home from "./pages/Home";
import Mission from "./pages/Mission";
import About from "./pages/About";
import Johnny from "./pages/Johnny"
import Shoutouts from "./pages/Shoutouts";
import { BrowserRouter as Router, Route, Routes } from "react-router-dom";

function App() {
  return (
    <div className="App">
      <Router basename="/boss/">
        <ScrollToTop/>
        <Navbar />
        <Routes>
          <Route path="/" exact element={<Home />} />
          <Route path="/values/" exact element={<Mission />} />
          <Route path="/founders/" exact element={<About />} />
          <Route path="/boss/" exact element={<Johnny />} />
          <Route path="/inspirations/" exact element={<Shoutouts />} />
          <Route path="*" exact element={<Home/>} />
        </Routes>
      </Router>
    </div>
  );
}

export default App;

下面是.htaccess文件的代码:

<IfModule mod_rewrite.c>

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

</IfModule>
hsvhsicv

hsvhsicv1#

@Jballin建议将basename更改为routes,这完全解决了问题。

相关问题