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

AWS Amplify重定向遇MIME类型错误:text/html被阻止

修复方案

1. 修正Amplify重定向规则

把跨域名跳转的规则改成301/302 Redirect(200 Rewrite仅适用于SPA内部路由),兜底规则保留200 Rewrite适配SPA:

Source addressTarget addressType
/abc/*https://domain2.com/abc/$1302 (Redirect)
/*/index.html200 (Rewrite)

注:Amplify的$1会匹配/*捕获的路径片段,保证跳转后路径完全一致

2. 对齐React路由与Vite Base配置

保留Vite的base: '/abc/',给React路由加上basename属性,统一路径前缀:

import { BrowserRouter, Routes, Route } from 'react-router-dom';

<BrowserRouter basename="/abc">
  <Routes>
    <Route path="/" element={<ABComponent receivedFailedMessages={setFailedMessages} startRecovery={startRecovery} />} />
    <Route path="*" element={<NotFound />} />
  </Routes>
</BrowserRouter>

这样路由会自动适配/abc的基础路径,无需在每个Route里重复写前缀。

3. 修复domain1的反向代理配置

如果domain1是通过反向代理指向domain2,确保所有/abc/*的请求(包括静态资源)都转发到domain2:
以Nginx为例,配置如下:

location /abc/ {
    proxy_pass https://domain2.com/abc/;
    proxy_set_header Host domain2.com;
    proxy_set_header X-Forwarded-For $remote_addr;
}

这条配置会把domain1.com/abc下的所有请求(包括/assets)都代理到domain2的对应路径,避免返回domain1自身的index.html

4. 验证构建与部署

  • 重新执行vite build,确认dist目录下的index.html中资源路径为/abc/assets/xxx(当前配置是正确的)
  • 确认amplify.yml的baseDirectory: dist配置无误,Amplify会正确部署dist目录的内容

为什么之前正常现在失效?

大概率是Amplify的重定向规则逻辑更新,或者domain1的反向代理配置被修改,导致原本正确的跨域跳转规则被误用为内部重写,触发SPA兜底规则返回index.html,进而导致静态资源的MIME类型错误。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.13 22:12:49