为我的React项目设置.htaccess文件 [英] Setting .htaccess file for my react project
问题描述
我在cpanel上部署了两个react build,基本上一个是管理面板,另一个是这样的网站:
I have deployed two react builds on cpanel, basically one is admin panel and second website like this:
admin文件夹由在/admin路由上运行的管理面板组成.客户文件夹包含在/路由上运行的网站的构建.
admin folder consist of build of admin panel working on /admin route. client folder consist of build of website working on / route.
我的.htaccess代码:
My .htaccess Code:
<IfModule mod_rewrite.c>
RewriteEngine on
# first check if request is in /client/
RewriteCond %{DOCUMENT_ROOT}/client%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}/client%{REQUEST_URI} -d
RewriteRule ^(.*)$ /client/$1 [L]
# then check if request is in /admin/
RewriteCond %{DOCUMENT_ROOT}/admin%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}/admin%{REQUEST_URI} -d
RewriteRule ^(.*)$ /admin/$1 [L]
</IfModule>
长时间搜索后,我发现此代码是我的应用程序的解决方案,因为在cpanel中部署了两个react应用程序.问题是,尽管我在任何路由页面上刷新,但路由都能正常运行,但会出现404错误.请帮忙.
After long searching I found this code as solution for my app as two react apps are deployed in cpanel. The problem is that whenever I refresh on any route page gives 404 error although routes work perfect. Please Help.
推荐答案
我在我的React应用程序中使用了这些规则,在我的情况下,我有一个React应用程序而不是两个
I'm using these rules for my react applications, in my cases I have one react application not two
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule . /index.html [L]
</IfModule>
这篇关于为我的React项目设置.htaccess文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!