最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Why does my reactjs project on hostinger give a 404 error when opening a new tab or refreshing the page? - Stack Ov

programmeradmin2浏览0评论

I have deployed reactjs project on hostinger where i open any new tab or refresh the page it give error 404

i have used latest version of react router dom

Index.js
<Browserrouter>
<App/>
<Browserrouter/>


App.js

<Routes>
<Route path="/" element={<Home/>}/>
        <Route path="/about-us" element={<Aboutus/>}/>
        <Route path="/contact-us" element={<Contactus/>}/>
        <Route path="/career" element={<Career/>}/>
        <Route path="/work" element={<Work/>}/> 
        <Route path="/services" element={<ServicesMain/>}/>

<Routes/>

I have deployed reactjs project on hostinger where i open any new tab or refresh the page it give error 404

i have used latest version of react router dom

Index.js
<Browserrouter>
<App/>
<Browserrouter/>


App.js

<Routes>
<Route path="/" element={<Home/>}/>
        <Route path="/about-us" element={<Aboutus/>}/>
        <Route path="/contact-us" element={<Contactus/>}/>
        <Route path="/career" element={<Career/>}/>
        <Route path="/work" element={<Work/>}/> 
        <Route path="/services" element={<ServicesMain/>}/>

<Routes/>
Share Improve this question edited Jul 26, 2023 at 14:25 Mayank Kumar Chaudhari 18.8k13 gold badges67 silver badges155 bronze badges asked Jun 2, 2023 at 13:39 Ashiq AvadiaAshiq Avadia 451 silver badge6 bronze badges 4
  • Your index is a bit suspect. Watch the Pascal casing on BrowserRouter, and you're not closing that ponent properly. It should be </BrowserRouter>. Does this work locally? Have you tested it? – Andy Commented Jun 2, 2023 at 13:42
  • 1 I have wrote this mannually...Its written in Pascal casing – Ashiq Avadia Commented Jun 2, 2023 at 13:49
  • This is because opening a new tab or refreshing does a new request to the server. On the root path / then it's fine, but when you go onto any routes beyond that, those paths aren't actually set up on hostinger, which simply hosts your entire built app at a single root route. I believe the options are to either use a hash router, or to have a hosting environment where you control the server (i.e. can implement a server that has a catch all route that just always returns your app, regardless of the route being requested). If you Google "react router 404 refresh" or similar, you can see deeper – Jayce444 Commented Jun 2, 2023 at 13:54
  • Should be able to fix this with a rewrite in .htaccess. You need to redirect / to /index.html – Kyle Lambert Commented Jun 2, 2023 at 14:03
Add a ment  | 

1 Answer 1

Reset to default 11

The issue is not with the React but your hosting config. You need to add rewrite rules by adding .htaccess file inside your 'public' folder with the following code.

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

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论