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

Laravel Inertia部署后登录页URL异常刷新出现404问题求助

问题排查与解决方案

1. 检查Inertia初始化配置

确认Vue项目中createInertiaApp的配置是否正确,避免错误添加路径前缀:

createInertiaApp({
  resolve: name => require(`./Pages/${name}`),
  setup({ el, App, props, plugin }) {
    return createApp({ render: () => h(App, props) })
      .use(plugin)
      .use(ZiggyVue)
      .mount(el);
  },
  // 手动指定根路径,覆盖自动检测的异常base URL
  baseUrl: '/',
});

同时检查根模板(如resources/views/app.blade.php)中的@inertia指令,确保没有被硬编码的路径包裹。

2. 修复服务器重写规则(刷新404核心解决点)

单页应用刷新404是因为服务器未将所有请求转发到index.php,需根据服务器类型配置:

  • Nginx 配置示例:
    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }
    
  • Apache 确保启用mod_rewrite,并在public/.htaccess中保留正确规则:
    <IfModule mod_rewrite.c>
        <IfModule mod_negotiation.c>
            Options -MultiViews -Indexes
        </IfModule>
    
        RewriteEngine On
    
        RewriteCond %{HTTP:Authorization} .
        RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
    
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteCond %{REQUEST_URI} (.+)/$
        RewriteRule ^ %1 [L,R=301]
    
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteRule ^ index.php [L]
    </IfModule>
    

若应用部署在子目录,需对应调整重写规则,但当前问题是URL自动添加/module,优先确认服务器是否误配置了子目录代理。

3. 验证Laravel与Ziggy配置

  • 确保.env中的APP_URL为https://mywebsite.com,无多余的/module后缀。
  • 在服务器上重新生成Ziggy路由,指定正确的生产环境URL:
    APP_URL=https://mywebsite.com php artisan ziggy:generate
    
    检查生成的resources/js/ziggy.js,确认所有路由的domain和path无异常前缀。

4. 规范前端跳转逻辑

确保登录页面跳转使用Inertia原生方式,避免混用原生跳转或Vue Router:

<!-- 正确的Inertia链接组件 -->
<Link href="/login">登录</Link>

<!-- 脚本中跳转 -->
import { router } from '@inertiajs/vue3';
router.visit('/login');

同时排查全局路由守卫、拦截器是否错误添加了路径前缀。

5. 清除所有缓存

服务器上执行Laravel缓存清理:

php artisan cache:clear
php artisan config:clear
php artisan route:clear

重新构建前端资源:

npm run build

确保生产环境静态资源无本地路径残留。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.07 16:27:27