“` 在子文件夹中嵌套的React应用的.htaccess文件 “`

huangapple go评论70阅读模式
英文:

.htaccess for nested React App within subfolders

问题

我正在尝试在子文件夹中嵌套两个React应用,并使用.htaccess进行重定向。

我无法访问根文件夹上的.htaccess。

这是我的目录结构 -

html/
├─ react-app-1/
│  ├─ react-app-2/
│  │  ├─ index.html
│  │  ├─ .htaccess
│  ├─ .htaccess
│  ├─ index.html
├─ 其他包含网站的子文件夹/...

react-app-1的路由器使用来自React文档的.htaccess文件完全正常

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

但是,每当我访问www.example.com/react-app-1/react-app-2时,我希望react-app-2提供它的index.html,但是URL被重定向到react-app-1的路由器。

我尝试了很多RewriteRules和RedirectMatch,但是每个其他规则都会导致react-app-1崩溃。

请提供用于.htaccess的RewriteRules的建议,以实现这一点。

谢谢。

尝试了多个.htaccess规则,包括RewriteCond、RewriteRule和RedirectMatch,但是每个其他规则都会导致react-app-1崩溃。

英文:

I am trying to host two React app nested within subfolders, with redirects using .htaccess.

I don't have access to the .htaccess on the root folder

Here is my directory structure -

html/
├─ react-app-1/
│  ├─ react-app-2/
│  │  ├─ index.html
│  │  ├─ .htaccess
│  ├─ .htaccess
│  ├─ index.html
├─ other_subfolders_with_websites/...

react-app-1 with Router works just fine with .htaccess from react-documentation

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

But, whenever I go to www.example.com/react-app-1/react-app-2, I want react-app-2 to serve it's index.html, but the URL is forwarded to Router of react-app-1.

I have tested lots of RewriteRules and RedirectMatch, but every other rule crashes react-app-1.

Please suggest RewriteRules for .htaccess to accomplish this.

Thank you.

Tried multiple .htaccess rules with RewriteCond, RewireRule and RedirectMatch, but every other rules crashes react-app-1.

答案1

得分: 0

RewriteRule ^ index.html [QSA,L]

将所有请求发送到 react-app1/.htaccess 中的 react-app1/index。

您可以在项目的根目录中使用一个 .htaccess 文件:

首先将指定的 react-app-2 请求发送到 react app2

RewriteRule ^react-app-2$ react-app-2/index.html [QSA,L]
RewriteRule ^react-app-2$/(.*) react-app-2/index.html [QSA,L]

如果请求不是针对 react app2

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

英文:
RewriteRule ^ index.html [QSA,L]

you send all request to react-app1/index in react-app1/.htaccess
you can use one htaccess file in root project :

#first send sepeficted react-app-2 request to react app2
RewriteRule ^react-app-2$ react-app-2/index.html [QSA,L]
RewriteRule ^react-app-2$/(.*) react-app-2/index.html [QSA,L]

#if request is not for react app2
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.html [QSA,L]

huangapple
  • 本文由 发表于 2023年2月8日 12:14:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/75381325.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定