如何使用Vite和React重定向Netlify中的路线?
我需要重定向到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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
尝试在根目录中创建
netlify.toml
文件。在 netlify.toml 中
Try creating a
netlify.toml
file in root directory.in netlify.toml
@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 thedist
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 todist
as is.Reference: https://vitejs.dev/guide/assets.html#the-public-directory
事实证明,我们需要在公共文件夹中找到_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
使用NetLify.toml文件
这对我有用。
参考
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
this worked for me.
Reference
Netlify redirect configuration Redirect options
Netlify configuration file (netlify.toml) Configuration file
您可以在root中创建相同的_redirect文件,然后修改构建脚本以将其添加到DIST文件夹中,如下
You could create the same _redirects file in the root and then modify build script to add it to dist folder like below