如何配置共享托管(例如HostGator)以向子页面显示我的React应用程序?

发布于 2025-01-27 13:09:50 字数 124 浏览 3 评论 0原文

我创建了一个带有ReactJ的页面,并且正在尝试将其上传到HostGator。我使用了React-Router-dom,可以访问主网站页面,但是当我尝试访问内部页面时,例如/about,我会收到404错误。

I created a page with reactJS and I am trying to upload it to Hostgator. I used react-router-dom and I can access the main website page, but when I try to access an inside page like /about I receive a 404 error.

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

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

发布评论

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

评论(2

萌酱 2025-02-03 13:09:50

您要寻找的模式称为a 前面控制器。您需要通过中央处理逻辑路由每个URL,以便可以选择在该URL上显示的内容。在React的情况下,该逻辑是在路由器组件中实现的。

共享托管(例如主机设备)通常使用Apache服务器。默认情况下,Apache将URL映射到本地文件系统。如果该文件在该路径上存在,则它可以使用它,否则会显示404错误。您需要覆盖此行为,以使您的React路由器能够完成工作。使用Apache,可以在名为.htaccess的文件中实现配置覆盖。

部署|创建React App 具有与index.html的同一目录中的Web主机上的.htaccess文件中的代码:

Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.html [QSA,L]

这会导致每个原因除非该名称的不同文件实际上存在于文件系统中,否则要通过index.html处理的单个URL。 index.html包括您的反应路由规则。

The pattern that you are looking for is called a front controller. You want to route every URL through central processing logic so that it can choose which content to display at that URL. In the case of React, that logic is implemented in the Router component.

Shared hosting such as Hostgator typically uses the Apache server. By default, Apache maps URLs to the local file system. If a file exists at that path, it serves it, otherwise it shows a 404 error. You need to override this behavior to get your React router to be able to do its job. With Apache, configuration overrides can be implemented in a file named .htaccess.

Deployment | Create React App has code that you need to put in a .htaccess file on your web host in the same directory as your index.html:

Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.html [QSA,L]

This causes every single URL to be handled by index.html unless a different file by that name actually exists in the file system. index.html includes your React routing rules.

樱&纷飞 2025-02-03 13:09:50

如果您需要在共享托管中重定向您的Create-React-App路由。您必须创建一个.htaccess文件,其中root index.html文件目录。

然后,您需要在.htaccess文件中代码,例如(下面的代码):

Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.html [QSA,L]

If you need to redirect your Create-react-app route in shared hosting. You have to Create a .htaccess file where your root index.html file directory.

Then you need to code in the .htaccess file such as(code below):

Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.html [QSA,L]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文