如何将React App正确部署到子目录?

发布于 2025-02-13 11:33:39 字数 2744 浏览 0 评论 0 原文

我通过在线遵循教程将React应用程序部署到子目录中,但是当我专门搜索该子目录时()或刷新子目录,它将我链接回主页。有人知道如何解决这个问题吗?谢谢。

代码。

{
  "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"
  }
}

是包装的

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;

<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>

I deployed my react app to a subdirectory by following tutorials online, but when I specifically search up that subdirectory (https://uuu.vip/boss) or refresh the subdirectory, it just links me back to the homepage. Would anyone know how to how to fix this? Thank you.

This is the code for package.json file:

{
  "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"
  }
}

This is the code for the App.js file:

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;

This is the code for the .htaccess file:

<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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧时模样 2025-02-20 11:33:39

@jballin建议将Basename更改为路线,这完全解决了问题。

@Jballin suggested to change the basename to routes, and this completely fixed the issue.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文