Laravel + Vite. Production build redirecting to /build path in url

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company

Laravel + Vite: Solving the Production Build Redirect Issue

As a senior developer working with the Laravel ecosystem, I often see a recurring headache when deploying front-end assets managed by Vite: the discrepancy between local development success and production deployment behavior. The scenario you've described—where building for production results in a redirect to /build instead of serving assets from the root domain (abc.com)—is a classic symptom of an incorrect base path configuration or asset loading setup.

This post will dive deep into why this happens and provide the definitive solution to ensure your Laravel application serves all assets directly from the root URL, eliminating unwanted prefixes like /build.

Understanding the Root Cause: Base Paths in Vite

When you run vite build, Vite compiles your assets (CSS, JavaScript) and places them in the output directory (usually public/build or similar). When Laravel is configured to handle these assets, it relies on a "base path" to correctly construct the URLs for those assets.

The redirect you are experiencing happens because, by default, if Vite or your asset loading configuration assumes a subdirectory structure, it prepends that path during the build process. If this assumption isn't properly overridden, Laravel's routing might interpret the resulting file structure as needing to be accessed under a specific subpath, leading to the unwanted redirect to /build.

Even though you experimented with the base configuration in vite.config.json, it seems the issue lies deeper, often involving how the assets are linked within your main Laravel entry point and the server serving process.

The Solution: Correctly Configuring Vite for Root Deployment

To force Vite to output assets that resolve correctly against the root domain, we need to explicitly tell Vite what the public base path is. Since you want everything served from abc.com/, the base path should be empty or set to /.

The key is ensuring that your asset references within your main application files (like resources/js/app.js or your Blade views) correctly interpret relative paths.

Step 1: Adjusting vite.config.js

While you mentioned trying the base configuration, let's ensure it is set correctly for a root deployment. For serving assets from the root domain, Vite should output paths that are relative to the root.

Review your vite.config.js. If you are deploying to a root directory, setting the base to / is crucial.

// vite.config.js
import laravel from "laravel-vite-plugin";
import { defineConfig } from "vite";
import vue from "@vitejs/plugin-vue";
// ... other imports

export default defineConfig({
    plugins: [
        vue(),
        laravel([
            // Ensure this points to your entry point
            "resources/js/main.ts" 
        ]),
        // ... other plugins
    ],
    base: '/', // <-- This is the critical setting for root deployment
});

Setting base: '/' tells Vite that all generated asset paths (like CSS imports or asset URLs) should be relative to the root of the domain. This prevents Vite from adding a subdirectory prefix during the build process, solving the redirection issue.

Step 2: Verifying Laravel Asset Loading

Ensure your main entry point file in your public assets correctly references these built files. In a standard Laravel setup using Vite, this is usually handled by the laravel-vite-plugin automatically injecting the necessary script tags into your Blade layout (app.blade.php).

If you have custom asset loading (e.g., manually linking CSS/JS), verify that these links use relative paths and not hardcoded absolute paths that might be misinterpreted during deployment. For robust front-end architecture, understanding how Laravel manages public assets is key, as seen in the best practices promoted by the Laravel Company documentation on asset management.

Step 3: Deployment and Cache Clearing

After making configuration changes, always perform a clean build and clear any cached files before deploying:

  1. Clean Build: Run vite build again to generate fresh assets.
  2. Deployment: Deploy the contents of your public directory.
  3. Cache Clearing: Clear Laravel's configuration and view caches (php artisan config:clear, php artisan view:clear) to ensure the server picks up the new asset paths correctly.

Conclusion

The redirection issue in a Laravel + Vite setup is almost always an environmental mismatch between the build output path and the public URL structure. By explicitly setting base: '/' in your vite.config.js and rigorously verifying your asset linking within the Laravel framework, you can ensure that your application serves all assets correctly from the root domain. Mastering these configuration details is fundamental to building scalable and reliable applications on the Laravel platform.