7

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?

0

5 Answers 5

8

Try creating a netlify.toml file in root directory.

in netlify.toml

[[redirects]]
  from = "/*"
  to = "/index.html"
  status = 200
Sign up to request clarification or add additional context in comments.

2 Comments

if this doesn't work... check THIS out.
it worked, but i had to manually add it in the dist (build) folder
5

@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

Comments

0

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

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
0

Using netlify.toml file

  1. Create netlify.toml file in the root of the repository
  2. Insert the below snippet in the file.

[[redirects]]
from="/*"
to="{base URL of your site}"
status=200

this worked for me.

Reference

Netlify redirect configuration Redirect options

Netlify configuration file (netlify.toml) Configuration file

Comments

-2

You could create the same _redirects file in the root and then modify build script to add it to dist folder like below

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

2 Comments

You could simply create the _redirects file inside the public folder
Back when this was answered the react vite plugin did not create public folder on setup, hence this was a workaround. Although you could create a public folder as Victor mentioned in his answer.