简体   繁体   中英

how to redirect routes in Netlify using vite and react?

I need to redirect to the home of a react project on Netlify but I'm using vite.

I used to do it using CRA (create-react-app) and creating the _redirects file in the public folder with this configuration.

/*  /index.html 200

How would I do it in vitejs?

Try creating a netlify.toml file in root directory.

in netlify.toml

[[redirects]]
  from = "/*"
  to = "/index.html"
  status = 200

您可以在根目录中创建相同的 _redirects 文件,然后修改构建脚本以将其添加到 dist 文件夹,如下所示

  "build": "vite build && cp ./_redirects ./dist/_redirects",

@sarthak 's answer is good, you just get to copy _redirects to the dist folder upon build. Although there is an easier way to do so.

Just create a folder named public and put your _redirects there. Everything in this folder will be copied to dist as is.

Reference: https://vitejs.dev/guide/assets.html#the-public-directory

Turns out, we need to locate _redirects file inside the public folder, which will be moved into dist folder .netlify considers it as the root when we use vite) automatically during build time

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM