Wrong laravel mix rewrite url path when place project in subfolder
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
Wrong Laravel Mix Rewrite URL Path When Placing Project in a Subfolder
Deploying modern Laravel applications, especially those integrating frontend frameworks like React via Laravel Mix, often presents subtle but frustrating pathing issues when placing the entire application within a subfolder on the server. This is a common hurdle when dealing with asset bundling and public file serving. As a senior developer, understanding how Webpack/Mix interacts with the Laravel public directory is crucial for seamless deployment.
If you are facing 404 errors for your compiled assets (like app.js or app.css) when deploying your application to a subdirectory (e.g., domain.com/patients), it almost always means that the paths being generated by Mix do not correctly account for the new base URL structure.
The Root of the Problem: Asset Path Misalignment
The issue stems from how Laravel Mix, by default, maps asset paths. When you use mix('path/to/file', 'public/destination'), it generates a path relative to the application's root public directory. If your application is served from /patients/, the browser requests assets from /css/app.css instead of the intended /patients/css/app.css.
In your provided setup, when you use mix('/js/app.js', 'public/js'), Mix generates an absolute path based on the root of the public folder. When deployed to a subdirectory, these paths break because they don't include the necessary segment (patients/).
The attempt to use mix.setResourceRoot('/laravel/public/') is a good instinct, but it often needs more precise configuration depending on your specific deployment environment and how Laravel handles asset versioning. We need a method that forces Mix to generate paths relative to the current URL context.
The Solution: Adjusting Asset Referencing and Configuration
The most reliable way to solve this is not just manipulating mix.setResourceRoot(), but ensuring that the Blade views reference assets using a path structure that respects the deployed subfolder. Furthermore, we can leverage Laravel's helper functions or adjust Mix’s output configuration if necessary.
1. Correcting the Blade References
Instead of relying solely on the raw asset paths generated by mix(), you should ensure your view references are relative to the application root, allowing the web server (or Laravel routing) to handle the path prefix.
If your assets are correctly placed in public/css and public/js within the subfolder, the Blade file must reflect this structure:
Corrected app.blade.php Example:
<!DOCTYPE html>
<html class="h-full bg-gray-200">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0">
<!-- Reference assets relative to the deployed subfolder -->
<link href="{{ asset('css/app.css') }}" rel="stylesheet">
<script src="{{ asset('js/app.js') }}" defer></script>
<title>Clinical Laboratory | Provider Portal</title>
@routes
</head>
<body class="font-sans leading-none text-gray-800 antialiased">
@inertia
</body>
</html>
By using the asset() helper, Laravel automatically prepends the correct public path base (which is defined by your web server configuration) to the relative file path. This bypasses the complexity of manually setting the resource root for standard asset loading.
2. Reviewing Webpack Configuration (For Advanced Control)
While the Blade fix often solves the immediate 404 issue, if you are running into deeper issues with asset hashing or bundling, you might need to adjust how Mix resolves paths within your webpack.mix.js. For deployment scenarios where assets are deeply nested, ensuring that your base path is correctly managed during compilation is key.
If you still encounter problems, ensure your configuration aligns with best practices promoted by the Laravel ecosystem. Understanding these underlying mechanics helps immensely when setting up complex applications, much like mastering the core concepts behind frameworks like those provided by https://laravelcompany.com.
Conclusion
The problem of incorrect asset pathing in subfolder deployments is a classic case of mismatched expectations between the build tool (Mix/Webpack) and the deployment environment (Laravel routing). The solution is to shift from relying on absolute paths compiled by Mix to using Laravel's powerful asset() helper within your Blade views. This ensures that your frontend assets are correctly served relative to the deployed application URL, providing a robust and maintainable structure for any Laravel project.