如何部署一个基于React路由器的应用到服务器?



我想部署react-router-based应用程序。当我部署我的网站的构建,它工作正常,但它不能做任何事情,当我打我的网站的任何URL。我的主要目的是我们如何告诉部署使用react-routesreact-router-dom的代码为

<Router>
{/* here I add some default component that show at every page */}
<div className="App">
<Navbar mode={"dark"} />
<Alert />
{/* here I add some default component that show at every page */}
{/* add some components that change as route change. */}
<Routes>
<Route path="/contact" element={<ContactUs />} />
<Route path="/city" element={<AllCities />} />
<Route path="/city/:cityName" element={<CityProfile />} />
<Route path="/city/:cityName/:articale" element={<Article />} />
<Route path="/news" element={<News />} />
<Route path="/about" element={<About />} />
<Route path="/Login" element={<Login />} />
<Route path="/Signup" element={<Signup />} />
<Route path="/User/:username" element={<User />} />
<Route path="/" element={[< LandingPage />, <TopCites />]} />
<Route path='/page not found' element={<NotFound />} />
<Route path='*' element={<NotFound />} />
</Routes>
{/* add some components that change as route change. */}
{/* routes end at here */}
<Footer />
</div >
</Router>

现在我如何告诉服务器在部署后使用这些路由?我的网站的URL是网站的URL,它的工作流程很顺利,但是当我点击我的网站的任何页面(网站的页面)的URL时,它显示page not found的错误。请告诉我如何处理这类问题。

确保创建具有以下重写规则的public/_redirects文件:

_redirectsin

/* /index.html  200

,然后重新部署您的站点并检查。

最新更新