Laravel部署后部分CSS/JS文件返回404问题求助
Hey there, let’s break down why some of your Laravel assets are returning 404s, especially since you’ve already ruled out permissions and confirmed other assets in the same folder load fine. Your deployment setup (app in a sibling folder to public_html, public content copied to public_html/subdomain) gives us some specific angles to check:
Double-check your APP_URL configuration
Theasset()helper relies on your.envfile’sAPP_URLto build resource paths. If yourAPP_URLdoesn’t include the subdomain (or subdirectory) path, it’ll generate URLs pointing to the root public_html instead of your subdomain folder. For example:- If your subdomain is
staging.yourdomain.com, setAPP_URL=https://staging.yourdomain.com - If it’s a subdirectory like
yourdomain.com/subdomain, setAPP_URL=https://yourdomain.com/subdomain
After updating, clear Laravel’s config cache withphp artisan config:cache(if you can run artisan commands on your server; if not, delete the files inbootstrap/cachemanually).
- If your subdomain is
Verify the .htaccess file in your subdomain folder
Hidden files like.htaccessoften get missed when copying the public directory contents. Laravel’s public/.htaccess handles URL rewriting, and without it, some assets might not be routed correctly.- Copy the
.htaccessfile directly from your Laravel app’spublic/folder topublic_html/subdomain/ - If you’re using Nginx instead of Apache, make sure your server block includes a location rule for the subdomain that routes requests properly:
location /subdomain { try_files $uri $uri/ /subdomain/index.php?$query_string; }
- Copy the
Clear browser and Laravel asset cache
Old cached URLs in your browser might be pointing to non-existent paths. Force a hard refresh (Ctrl+Shift+R on Windows/Linux, Cmd+Shift+R on Mac) to bypass browser cache. For longer-term fixes, add a version hash to your asset calls to bust cache automatically when files change:asset('css/custom.css?v=' . filemtime(public_path('css/custom.css')))Check for filename case sensitivity
Linux servers (most production environments) are case-sensitive for filenames. If your code referencesasset('css/Header.css')but the actual file isheader.css, it’ll throw a 404 (even if it works on your Windows dev machine). Double-check that your asset paths match the exact case of the files in your subdomain folder.
内容的提问来源于stack exchange,提问作者Martijn

