共享主机部署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:
Method 1: Adjust Directory Structure (Recommended for Security)
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 thepublicfolder) to a directory abovepublic_html(name it something likelaravel-appfor clarity). This keeps sensitive files likeapp/,bootstrap/, andvendor/safe from web access. - Next, move all contents of
public_html/project/public(includingindex.php,.htaccess, and your assets likecss//js/) directly intopublic_html(overwrite any existing files if needed—this becomes your new web root entry point). - Finally, edit the
index.phpfile inpublic_htmlto 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
.htaccessfile in yourpublic_htmlroot 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.phpfile inpublic_html/project/publicto update the paths to correctly point to the Laravel core files:
Note:// 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';__DIR__here refers topublic_html/project/public, so../../takes us back topublic_html, then/project/vendorgets us to the correct location. - Keep your existing
.htaccessinpublic_html/project/publicas-is—it handles Laravel's internal routing.
Final Checks
After setting up either method:
- Verify that
mod_rewriteis enabled on your host (most shared hosts have this on by default). - Ensure the
storage/andbootstrap/cache/directories in your Laravel core have proper write permissions (usually755or775depending on your host setup).
内容的提问来源于stack exchange,提问作者Vahn Marty Cagalawan
相关产品推荐
相关产品推荐

