How to make public folder as root in Laravel?
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
When developing web applications using the Laravel framework, you may find your public folder being accessed through https://example.com/public instead of simply https://example.com. This can be a bit cumbersome and unappealing from a user's perspective. In this blog post, we will discuss the reasons behind this behavior and explore methods to make the public folder act as your application's root directory.
Understanding Laravel's Public Folder
In Laravel applications, access to all static assets like images, CSS stylesheets, and JavaScript files is routed through the public folder. This is because Nginx or Apache web servers handle these static content requests for optimal performance and reliability. The public folder makes it easier to separate your website's back-end code from its front-end elements.
Making Public Folder As Root with Directory Index
The quickest way to get rid of the /public in your URLs is by using a directory index. A directory index is an HTML page that serves as a starting point for visitors who access your website without specifying any further path or file name. Here's how you can create and configure such an index:
Create an
index.phpfile in the root directory of your Laravel application.Write a simple PHP code that automatically redirects to the public folder's index page:
if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') { header('HTTP/1.1 403 Forbidden'); exit; } else { header('Location: /public/index.php'); exit; }Save the file and place it in the root directory of your Laravel application.
Now, when users visit your website at
http://example.com/, they will be redirected to their intended destination:https://example.com/public/index.php. This effectively changes the public folder as your application's root directory in terms of user experience.
Alternative Approach: Configure VirtualHost for Laravel Applications
Another approach to this issue is configuring a virtual host file on your server. This method involves creating a virtual host file that maps the root directory of your Laravel application to its actual root folder, bypassing the need for an index file altogether.
Create a virtual host configuration file and save it as
000-default.confin the Nginx (or Apache) sites-available folder on your server.Add the following content to the file:
server { listen 80; server_name example.com; root /path/to/laravelapp/public; location / { try_files $uri $uri/ =404; } }Replace
/path/to/laravelapp/publicwith the path to your Laravel application's public folder.Save the file and enable the changes by running the following command:
sudo a2ensite 000-default.confRestart your web server to apply the changes, and then visit
http://example.com/. Now, your Laravel application behaves as if its public folder is the root directory.
Conclusion
By following one of these approaches - either by creating a simple index file or configuring a virtual host file on your server - you can make the /public folder act as your application's root directory for easier user experience. Ensure to test thoroughly and choose the method that best fits your specific needs. Always back up your work before making any changes, especially if you are working with production environments.