You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

无根目录.htaccess权限,嵌套子目录双React应用重写规则求助

解决嵌套React应用的.htaccess路由冲突问题

问题核心是react-app-1的现有规则会拦截所有指向/react-app-1/react-app-2的请求,将其转发到react-app-1的index.html,而非交给react-app-2的规则处理。以下是可行的配置方案:

1. 修改react-app-1/.htaccess

在原有规则中添加排除条件,让react-app-2路径下的请求能传递到自身的.htaccess处理:

Options -MultiViews
RewriteEngine On

# 排除react-app-2路径下的所有请求
RewriteCond %{REQUEST_URI} !^/react-app-1/react-app-2/
# 仅当请求不是文件时,转发到react-app-1的index.html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.html [QSA,L]

2. 配置react-app-2/.htaccess

保持SPA路由的基础规则,确保它能处理自身目录下的非文件请求:

Options -MultiViews
RewriteEngine On

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

3. 配置React Router的basename

为了让react-app-2的路由正确识别路径,需要在其Router组件中设置basename属性:

// react-app-2的路由配置文件
import { BrowserRouter as Router } from 'react-router-dom';

function App() {
  return (
    <Router basename="/react-app-1/react-app-2">
      {/* 你的路由内容 */}
    </Router>
  );
}

完成以上配置后,访问www.example.com/react-app-1/react-app-2及其子路径时,请求会由react-app-2的index.html处理,同时react-app-1的其他路径仍能正常运行。

内容的提问来源于stack exchange,提问作者jayanta525

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.02 00:01:14