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

共享主机部署Laravel:实现域名直接访问项目的问题求助

Fix Laravel Access to Root Domain (Instead of /project/public)

Got it, let's get your Laravel app loading directly at http://example.com/ instead of having to use the /project/public suffix. Based on your current directory structure (public_html → project → public), here are two proven solutions:

This aligns with Laravel's official best practice—keeping your app's core files outside the web root (so they can't be accessed directly via the browser).

  • First, move all the core Laravel files/folders from public_html/project (everything except the public folder) to a directory above public_html (name it something like laravel-app for clarity). This keeps sensitive files like app/, bootstrap/, and vendor/ safe from web access.
  • Next, move all contents of public_html/project/public (including index.php, .htaccess, and your assets like css//js/) directly into public_html (overwrite any existing files if needed—this becomes your new web root entry point).
  • Finally, edit the index.php file in public_html to update the paths to point to your new core directory:
    // Replace these lines:
    require __DIR__.'/../vendor/autoload.php';
    $app = require_once __DIR__.'/../bootstrap/app.php';
    
    // With these (adjust "laravel-app" to match your actual folder name):
    require __DIR__.'/../laravel-app/vendor/autoload.php';
    $app = require_once __DIR__.'/../laravel-app/bootstrap/app.php';
    

Method 2: Use .htaccess Rewrite (No File Moving Needed)

If you can't modify directories above public_html (some shared hosts restrict this), use this rewrite approach:

  • Create or edit the .htaccess file in your public_html root directory, adding these rules:
    <IfModule mod_rewrite.c>
        RewriteEngine On
        # Redirect all requests to project/public
        RewriteRule ^$ project/public/ [L]
        RewriteRule (.*) project/public/$1 [L]
    </IfModule>
    
  • Then, edit the index.php file in public_html/project/public to update the paths to correctly point to the Laravel core files:
    // Replace these lines:
    require __DIR__.'/../vendor/autoload.php';
    $app = require_once __DIR__.'/../bootstrap/app.php';
    
    // With these:
    require __DIR__.'/../../project/vendor/autoload.php';
    $app = require_once __DIR__.'/../../project/bootstrap/app.php';
    
    Note: __DIR__ here refers to public_html/project/public, so ../../ takes us back to public_html, then /project/vendor gets us to the correct location.
  • Keep your existing .htaccess in public_html/project/public as-is—it handles Laravel's internal routing.

Final Checks

After setting up either method:

  • Verify that mod_rewrite is enabled on your host (most shared hosts have this on by default).
  • Ensure the storage/ and bootstrap/cache/ directories in your Laravel core have proper write permissions (usually 755 or 775 depending on your host setup).

内容的提问来源于stack exchange,提问作者Vahn Marty Cagalawan

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 08:01:09