How to make public folder as root in Laravel?

How to make public folder as root in Laravel?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

How to Make the Public Folder the Root in Laravel

Laravel applications are architected with a specific entry point: the public directory. By default, your web server serves files from this folder, and the actual application logic begins executing inside index.php. This structure creates a deliberate separation between static assets and the framework core, but it often results in URLs that include /public, such as https://example.com/public/home. If you prefer to serve your application directly from the root domain (https://example.com), you need to adjust your web server configuration rather than relying on simple HTTP redirects.

A common workaround involves creating a generic index.php file in the project's root directory that issues a 301 redirect to /public. While functional, this approach is technically inelegant. It clutters your repository with unnecessary files and forces users to see the /public path even after the redirect completes if they inspect browser history or bookmarks. More importantly, it bypasses the web server's native ability to route requests correctly, which can complicate caching strategies and security rules.

The robust solution lies in configuring your web server (Apache or Nginx) to serve the public directory as the document root without moving the entire application structure. This allows you to keep Laravel's internal organization intact while presenting a clean URL to the end-user.

Apache Configuration

If you are running Apache, you can achieve this by modifying your virtual host configuration. The key is to change the DocumentRoot directive to point directly at your project's public folder and ensure that Apache does not try to serve the parent directory's index file when a request hits /.

Assuming your project resides in /var/www/myapp, you would update your .htaccess or virtual host config to look like this:

<VirtualHost *:80>
    ServerName example.com
    DocumentRoot /var/www/myapp/public
    
    <Directory /var/www/myapp/public>
        AllowOverride All
        Require all granted
    </Directory>
</VirtualHost>

By setting DocumentRoot to /var/www/myapp/public, Apache will treat the contents of that folder as if they were at the root level. Consequently, a request to https://example.com/ will be served by public/index.php.

If you cannot modify the main server configuration and must rely on .htaccess, you can add a rule to handle requests for missing files in the parent directory before falling back to the standard Laravel routing. However, the cleanest method is ensuring the web server serves only the public folder. You may also need to ensure that mod_rewrite is enabled and that your existing .htaccess file inside the public folder contains the standard Laravel directives:

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews -Indexes
    </IfModule>

    RewriteEngine On

    # Handle Authorization Header
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} (.+)/$
    RewriteRule ^ %1 [L,R=301]

    # Send Requests To Front Controller
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>

Nginx Configuration

For Nginx users, the configuration is handled in the server block. You simply point the root directive to your public directory and ensure that all requests are proxied or served from there.

server {
    listen 80;
    server_name example.com;
    
    root /var/www/myapp/public;
    index index.php;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }
}

The try_files directive is crucial here. It attempts to serve the requested file or directory first. If that fails, it falls back to routing the request to index.php, which triggers Laravel's router. This ensures that static assets like images and CSS load directly from the web server (faster), while dynamic routes are handled by PHP.

Important Considerations

When making the public folder your root, you must be careful not to expose sensitive files. The public directory should only contain files intended for direct browser access: HTML, CSS, JavaScript, images, and the index.php. Ensure that directories like .git, vendor, node_modules, and configuration files remain outside the document root or are explicitly blocked via web server rules.

This approach preserves Laravel's security model. The framework expects to run from within a public directory; moving it elsewhere without proper server configuration can sometimes lead to accidental exposure of core files if your .htaccess or Nginx rules aren't strict enough. By configuring the document root correctly, you maintain the intended separation between static assets and application logic while achieving the desired URL structure.

Our network