Remove /public from URL in Laravel Project – Best Methods for Clean URLs

In this post, you’ll learn how to remove /public/ from your Laravel URL using three effective methods. We’ll cover local development, production hosting, and some gotchas to avoid.

Remove /public from URL in Laravel Project – Best Methods for Clean URLs Image

If you’re building a Laravel app and see URLs like http://example.com/public/, you're not alone. Laravel routes through the public/ directory by default for security reasons, but it’s totally possible (and clean) to remove /public/ from the URL.

Why Laravel Uses /public in URLs

Laravel’s public/ folder acts as the entry point for the application. It keeps your core code (app/, routes/, .env) safe from public access by routing all web traffic through a single secure location: public/index.php.

While this is great for security, it’s not ideal for clean URLs.


Method 1: Move Files from /public to Root (Not Recommended for Production)

This method involves moving key files from /public to your Laravel root.

Steps:

  1. Move these files from public/ to your root:

    • index.php

    • .htaccess

  2. Open the index.php file and update the paths:

    // Before require __DIR__.'/../vendor/autoload.php';
    $app = require_once __DIR__.'/../bootstrap/app.php';
    // After require __DIR__.'/vendor/autoload.php';
    $app = require_once __DIR__.'/bootstrap/app.php';
    

⚠️ Warning: This exposes your app to potential risks. Only use this for temporary setups or local experiments.


Method 2: Use a Virtual Host (Recommended)

The best and secure method is to configure a virtual host that points directly to the public/ directory.

- Virtual Host Config in Apache (XAMPP, WAMP, or VPS):

<VirtualHost *:80>
    ServerName laravel.test
    DocumentRoot "/path-to-your-project/public"
    <Directory "/path-to-your-project/public">
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>

Then:

  • Add 127.0.0.1 laravel.test to your /etc/hosts or C:\Windows\System32\drivers\etc\hosts

  • Restart Apache

You can now visit your app at http://laravel.test without /public.

- Nginx (Bonus Tip)

Point the root to public/ in your site config:

root /var/www/your-laravel-project/public;

Method 3: Redirect Using .htaccess

If you can’t set up virtual hosts (like on shared hosting), use an .htaccess file in your Laravel root:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^$ public/index.php [L]
    RewriteRule ^((?!public/).*)$ public/$1 [L,NC]
</IfModule>

This will silently rewrite all your base URIs to the /public folder. Even all Headers, for example the HTTP Authorization Header, and all optional URI parameters will silently be passed on to the /public folder as well.

This isn’t a clean solution—it just forwards requests to public/, but the URL won’t show it.


Which Method is Best?

MethodBest ForSEO FriendlySecure
Move FilesQuick demo
Virtual HostLocal & production
.htaccessShared hosting⚠️ Partial⚠️ Partial

Use virtual hosts or server configs for professional and production-grade setups.


Related Resources


FAQ

Q: Why not just delete the public/ folder?

Because Laravel uses index.php in public/ to bootstrap the app securely. Removing it breaks the routing and exposes critical files.

Q: Is it safe to move index.php to root?

Only for local testing. In production, it’s insecure and not recommended.

Q: How to set up Laravel Valet to remove /public?

Valet automatically serves from the public/ directory. Just run valet link in your project folder.


Conclusion

Removing /public/ from your Laravel URL isn’t just about aesthetics—it’s about serving your app cleanly and securely. 

For best results, use virtual hosts or Nginx/Apache config to serve from the public/ folder directly.

Do you Like?