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

在IIS服务器部署Flutter Web时,除index.html外所有页面均报404错误

问题原因与解决方案

Flutter Web本质是单页应用(SPA),所有页面路由逻辑都封装在main.dart.js中,服务器端并没有对应/homePage这类路径的物理文件。当你直接访问这类子路径时,服务器会尝试寻找对应的文件,找不到就返回404;而访问根路径/时,服务器会加载index.html,进而加载Flutter的前端逻辑,此时前端路由才能正常工作。

你需要配置服务器,将所有无法匹配到物理文件的请求转发到index.html,让Flutter前端来处理路由。以下是常见服务器的配置方式:

Nginx配置

修改站点配置文件,在location /块中添加try_files指令:

location / {
  try_files $uri $uri/ /index.html;
}

Apache配置

  1. 确保启用mod_rewrite模块
  2. 在项目根目录创建/修改.htaccess文件,添加以下规则:
RewriteEngine On
RewriteBase /
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.html [L]

Firebase Hosting配置

在firebase.json中配置rewrites规则:

{
  "hosting": {
    "public": "build/web",
    "rewrites": [
      {
        "source": "**",
        "destination": "/index.html"
      }
    ]
  }
}

补充说明

如果你的Flutter Web应用部署在子目录下(如https://xxxx.com/myapp/),构建时需要指定base href:

flutter build web --base-href="/myapp/"

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.19 09:57:07