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

Laravel部署后部分CSS/JS文件返回404问题求助

Troubleshooting Laravel Asset 404s in Subdomain Deployment

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
    The asset() helper relies on your .env file’s APP_URL to build resource paths. If your APP_URL doesn’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, set APP_URL=https://staging.yourdomain.com
    • If it’s a subdirectory like yourdomain.com/subdomain, set APP_URL=https://yourdomain.com/subdomain
      After updating, clear Laravel’s config cache with php artisan config:cache (if you can run artisan commands on your server; if not, delete the files in bootstrap/cache manually).
  • Verify the .htaccess file in your subdomain folder
    Hidden files like .htaccess often 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 .htaccess file directly from your Laravel app’s public/ folder to public_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;
      }
      
  • 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 references asset('css/Header.css') but the actual file is header.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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.22 08:22:38