Laravel 5.5本地部署:如何绕过/public目录直接访问项目
Got it, let's get this sorted so you can reach your Laravel 5.5 project directly via http://localhost/my_laravel/ instead of having to append /public every time. Here are two reliable methods tailored for local setups like XAMPP or WAMP:
Method 1: Use .htaccess Rewrite (Quick & Simple for Apache)
This is the fastest fix if you want to keep your existing URL structure:
- Head to your project root folder (
my_laravel). If there’s no.htaccessfile here, create a new one. - Open the
.htaccessfile and paste this code:
<IfModule mod_rewrite.c> RewriteEngine On RewriteRule ^(.*)$ public/$1 [L] </IfModule>
- Save the file and restart your Apache server. Now when you visit
http://localhost/my_laravel/, it’ll automatically route to the/publicdirectory behind the scenes.
Quick check: Make sure Apache’s mod_rewrite module is enabled. Open your httpd.conf file (in XAMPP/WAMP’s Apache folder) and remove the # from this line if it’s present:
#LoadModule rewrite_module modules/mod_rewrite.so
Restart Apache to apply the change.
Method 2: Configure Virtual Host (Cleaner & Scalable)
If you prefer a more professional local URL (like http://mylaravel.local) and want a setup closer to production, try this:
- Open Apache’s
httpd-vhosts.conffile (usually inxampp/apache/conf/extra/orwamp/bin/apache/apacheX.X.X/conf/extra/). - Add this virtual host config at the end:
<VirtualHost *:80> DocumentRoot "C:/xampp/htdocs/my_laravel/public" # Replace with your actual project public path ServerName mylaravel.local </VirtualHost>
- Edit your system’s
hostsfile:- Windows: Go to
C:\Windows\System32\drivers\etc\hosts(you’ll need admin rights to edit). - Mac/Linux: Open
/etc/hostsin a text editor with sudo privileges.
- Windows: Go to
- Add this line to the hosts file:
127.0.0.1 mylaravel.local
- Save all files and restart Apache. Now you can access your project via
http://mylaravel.localdirectly—no need forlocalhost/my_laravelat all!
Either method will solve your problem. The first is perfect for keeping your current URL, while the second gives you a cleaner, production-like local environment.
内容的提问来源于stack exchange,提问作者Amit Kumar Singh

