共享主机下React集成CodeIgniter4:API请求被拦截/404错误
React前端与CodeIgniter 4后端集成的API路由问题
我正在将React前端与CodeIgniter 4后端部署到共享主机环境中,遇到了API路由和服务器配置相关的问题。试过多种.htaccess配置及排障步骤,但前端发起的API请求要么返回404错误,要么被React拦截返回HTML内容。
项目目录结构
/public_html/ │ ├── /instructor_frontend/ │ └── /build/ │ ├── /instructor_web_new/ │ └── /public/ │ └── .htaccess │ └── .htaccess
目标
- 从
/instructor_frontend/build/index.html提供React应用服务 - 将所有
/api请求转发至CodeIgniter后端(/instructor_web_new/public/index.php) - 确保前端请求由React处理,API请求由CodeIgniter处理
遇到的问题
- API请求被React拦截:访问
instructor.greenhorndriving.com/api/test时返回200状态码,但得到的是React应用的HTML内容,而非CodeIgniter的预期响应。 - 构建后出现404错误:运行
npm run build构建React应用后,/instructor_frontend/build/下的.htaccess会被重置为默认配置,导致API请求无法正确路由,出现404错误。
React构建后的默认.htaccess(instructor_frontend/build/目录下)
RewriteEngine on RewriteCond %{HTTPS} off RewriteCond %{HTTP:X-Forwarded-SSL} !on RewriteCond %{HTTP_HOST} ^instructor\.greenhorndriving\.com$ [OR] RewriteCond %{HTTP_HOST} ^www\.instructor\.greenhorndriving\.com$ RewriteRule ^/?$ "https\:\/\/instructor\.greenhorndriving\.com\/" [R=301,L]
我修改后的build目录.htaccess
<IfModule mod_rewrite.c> RewriteEngine On RewriteBase / # 转发API请求到CodeIgniter RewriteCond %{REQUEST_URI} ^/api RewriteRule ^api/(.*)$ /instructor_web_new/public/index.php?/$1 [L,QSA] # 其他请求交给React处理 RewriteCond %{REQUEST_URI} !^/api RewriteRule ^ index.html [L] # HTTPS重定向 RewriteCond %{HTTPS} off RewriteCond %{HTTP:X-Forwarded-SSL} !on RewriteCond %{HTTP_HOST} ^instructor\.greenhorndriving\.com$ [OR] RewriteCond %{HTTP_HOST} ^www\.instructor\.greenhorndriving\.com$ RewriteRule ^/?$ "https://instructor.greenhorndriving.com/" [R=301,L] </IfModule>
修改后访问/api/test仍返回React的HTML内容,而非后端预期响应。
我尝试的根目录/public_html/.htaccess配置
# 直接返回静态资源(比如manifest.json) RewriteCond %{HTTP_HOST} ^instructor\.greenhorndriving\.com$ [NC] RewriteCond %{REQUEST_FILENAME} -f RewriteRule ^(.*)$ - [L] # 转发API请求到CodeIgniter RewriteCond %{HTTP_HOST} ^instructor\.greenhorndriving\.com$ [NC] RewriteCond %{REQUEST_URI} ^/api RewriteRule ^api/(.*)$ /instructor_web_new/public/index.php/$1 [L,QSA] # 其余请求交给React应用 RewriteCond %{HTTP_HOST} ^instructor\.greenhorndriving\.com$ [NC] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ /instructor_frontend/build/index.html [L]
该配置同样没有效果。
CodeIgniter配置
路由文件(app/Config/Routes.php)
$routes->get('/', 'Home::index'); $routes->get('api/test', function() { return "Work Pls...."; }); $routes->group('api', function($routes) { $routes->post('login', 'AuthController::login'); $routes->post('logout', 'AuthController::logout'); });
Auth控制器(app/Controllers/AuthController.php)
namespace App\Controllers; use App\Models\UserModel; use CodeIgniter\RESTful\ResourceController; class AuthController extends ResourceController { public function login() { $model = new UserModel(); $email = $this->request->getPost('email'); $password = $this->request->getPost('password'); $encodedPassword = base64_encode($password); $device_type = $this->request->getPost('device_name'); $device_token = $this->request->getPost('device_token'); $user = $model->where('email', $email) ->where('role', 'instructor') ->where('auth_level', 3) ->first(); if ($user) { if ($user['user_status'] == 1) { if ($encodedPassword === $user['password']) { $model->update($user['id'], [ 'device_name' => $device_type, 'device_token' => $device_token ]); session()->set([ 'user_id' => $user['id'], 'email' => $user['email'], 'role' => $user['role'] ]); return $this->respond([ 'status' => 1, 'message' => 'Logged in successfully.', 'data' => [ 'email' => $user['email'], 'role' => $user['role'] ] ], 200); } else { return $this->respond(['status' => 0, 'message' => 'Invalid credentials.'], 401); } } else { return $this->respond(['status' => 0, 'message' => 'Your account has been Inactive, Please contact the administrator.'], 403); } } else { return $this->respond(['status' => 0, 'message' => 'This email is not registered.'], 401); } } public function logout() { session()->destroy(); return $this->respond(['status' => 1, 'message' => 'Logged out successfully.'], 200); } }
核心疑问
为什么修改.htaccess后,/api请求还是被React拦截,没有路由到CodeIgniter?
服务器已启用mod_rewrite,Apache虚拟主机配置了AllowOverride All指令。
内容的提问来源于stack exchange,提问作者Blaze
相关产品推荐
相关产品推荐

