如何使用Vite和React重定向Netlify中的路线?

发布于 2025-01-28 18:15:35 字数 203 浏览 3 评论 0原文

我需要重定向到Netlify上的React项目的房屋,但我正在使用Vite。

我曾经使用CRA(create-react-app)进行此操作,并使用此配置在公共文件夹中创建 _redirects 文件。

/*  /index.html 200

我将如何在Vitejs中做到这一点?

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?

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

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

发布评论

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

评论(5

又爬满兰若 2025-02-04 18:15:35

尝试在根目录中创建netlify.toml文件。

netlify.toml

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

Try creating a netlify.toml file in root directory.

in netlify.toml

[[redirects]]
  from = "/*"
  to = "/index.html"
  status = 200
情绪操控生活 2025-02-04 18:15:35

@sarthak 的答案很好,您只需复制_redirects构建时Dist 文件夹。尽管有一种更简单的方法可以这样做。

只需创建一个名为public的文件夹,然后将您的_redirects放在那里。此文件夹中的所有内容都将被复制到dist

Reference: https://vitejs.dev/guide/assets.html#the-公共目录

@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

探春 2025-02-04 18:15:35

事实证明,我们需要在公共文件夹中找到_redirects文件,该文件将在构建时间内自动将其移至DIST文件夹中(NetLify将其视为根部)

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

时光礼记 2025-02-04 18:15:35

使用NetLify.toml文件

  1. 在存储库的根部
  2. create netlify.toml文件插入文件中的下图。

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

这对我有用。

参考

NetLify重定向配置 redirect options

netlify配置文件(netlify.toml) a href =“ https://docs.netlify.com/routing/redirects/#syntax-for-the-netlify-configuration-file-file” rel =“ nofollow noreferrer”>配置文件

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

红焚 2025-02-04 18:15:35

您可以在root中创建相同的_redirect文件,然后修改构建脚本以将其添加到DIST文件夹中,如下

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

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