Fix Laravel URLs: Remove Public from Your Website Address
When you install Laravel on your server or localhost, you might notice that your project URL includes /public
, like this:
http://example.com/public
This happens because Laravel places its main entry point inside the public
folder for security reasons. However, having /public
in the URL looks unprofessional and is not user-friendly.
- Fix Laravel URLs: Remove Public from Your Website Address
- Why Does Laravel Include the /public Directory in the URL?
- Method 1: Move Contents of Public Folder to Root (Not Recommended)
- Method 2: Use an .htaccess File (Recommended for Shared Hosting)
- Method 3: Change the Document Root in cPanel or Apache Configuration
- Method 4: Create a Symbolic Link (Advanced Users)
- FAQs on How to Remove /public from Laravel URL
- Conclusion
In this guide, I’ll show you step-by-step how to remove /public
from your Laravel URL properly and securely.
Suggested Reading: How to Deploy Laravel Project on cPanel
Why Does Laravel Include the /public Directory in the URL?
Laravel is designed with security in mind. The public
directory is the only folder that should be directly accessible by users because it contains the front-facing assets like CSS, JavaScript, and index.php. Meanwhile, the rest of the Laravel application (controllers, models, views, and configuration files) remains secure outside the web root to prevent direct access.
Now, let’s dive into different methods to remove /public
from your Laravel URL.
Method 1: Move Contents of Public Folder to Root (Not Recommended)
A quick but risky way to remove /public
from the URL is to move all files inside the public
folder directly into your root Laravel directory. However, this method is not recommended because it exposes your application files to potential security threats.
If you still want to proceed, follow these steps:
- Move everything inside the
public
folder (except.htaccess
) to the root folder. - Open the
index.php
file and modify these lines:
require __DIR__.'/../bootstrap/autoload.php';
$app = require_once __DIR__.'/../bootstrap/app.php';
Change them to:
require __DIR__.'/bootstrap/autoload.php';
$app = require_once __DIR__.'/bootstrap/app.php';
⚠ Warning: While this works, it is not safe because it exposes critical Laravel directories like routes
, storage
, and config
to the public. Avoid using this method unless absolutely necessary.
Method 2: Use an .htaccess File (Recommended for Shared Hosting)
The best way to remove /public
from your Laravel URL is by using an .htaccess
file. This method is safe and works well on shared hosting environments like cPanel.
Steps to Use .htaccess to Remove /public from URL
- Create or edit the .htaccess file in the root Laravel folder.
- Add the following rewrite rules:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
- Save the file and upload it to your server (if applicable).
Now, when you visit http://example.com
, it will automatically serve the Laravel application without requiring /public
in the URL.
Method 3: Change the Document Root in cPanel or Apache Configuration
If you have full control over your hosting environment (such as VPS or Dedicated servers), a cleaner solution is to change your server’s document root to the public
folder.
For cPanel Users
- Log into cPanel and go to Domains > Manage.
- Locate your domain and find the Document Root section.
- Change the document root from
/home/youruser/public_html/
to/home/youruser/public_html/public
. - Save the changes and restart Apache if necessary.
For Apache Users (VPS/Dedicated Server)
- Open your Apache configuration file (
httpd.conf
or your virtual host file). - Find the
<VirtualHost>
block for your domain and update theDocumentRoot
:
DocumentRoot "/var/www/html/public"
<Directory "/var/www/html/public">
AllowOverride All
Require all granted
</Directory>
- Save the file and restart Apache:
sudo systemctl restart apache2
For Nginx Users
- Open your Nginx configuration file:
sudo nano /etc/nginx/sites-available/default
- Change the
root
directive:
root /var/www/html/public;
- Save the file and restart Nginx:
sudo systemctl restart nginx
This method ensures that your Laravel app loads correctly without needing an .htaccess
workaround.
Method 4: Create a Symbolic Link (Advanced Users)
Another clean way to remove /public
is by creating a symbolic link that points the public_html
folder to the Laravel public
directory.
Steps to Create a Symbolic Link
- Backup your current
html
directory:
mv html html_backup
- Create a symbolic link from
html
to Laravel’spublic
folder:
ln -s /home/youruser/laravel_project/public /home/youruser/html
- Verify the symbolic link:
ls -l /home/youruser/html
This approach keeps your Laravel structure intact while making the public files accessible through html
.
FAQs on How to Remove /public from Laravel URL
/public
directory in the URL?Laravel keeps application files outside the web root for security reasons. The public
directory is designed to serve assets while keeping critical files like routes, controllers, and configuration files protected from direct access.
/public
from the Laravel URL?The simplest and safest way is to use an .htaccess
file with a rewrite rule that redirects traffic to the public
folder. This method works well for shared hosting and requires minimal configuration.
public
directory to the root folder?While possible, it is not recommended due to security risks. Moving these files exposes your Laravel app’s internal structure, making it vulnerable to attacks. Instead, use a symbolic link or change the document root.
public
folder as the document root in cPanel?In cPanel, go to Domains > Manage, find the document root setting, and change it to /home/youruser/public_html/public
. Save the changes, and Laravel should work without /public
in the URL.
/public
?Ensure that .htaccess
is correctly placed in the root directory and that mod_rewrite is enabled on your Apache server. Also, check that your server.php
file correctly directs requests to public/index.php
.
/public
from the URL?Yes! Modify the Nginx configuration file (/etc/nginx/sites-available/default
) and set the root
directive to /var/www/html/public
. Restart Nginx, and Laravel should load without /public
.
Conclusion
Removing /public
from your Laravel URL is essential for a professional-looking and user-friendly website. Depending on your hosting setup, you can use:
✅ .htaccess Rewrite Rules – Best for shared hosting and quick fixes.
✅ Changing Document Root – Ideal for cPanel, VPS, or dedicated servers.
✅ Creating a Symbolic Link – Useful for keeping Laravel’s structure clean.
Avoid moving files out of the public
directory, as it poses security risks.
I hope this guide helps you set up your Laravel project the right way! If you have any questions, reach out to Aveshost support or drop a comment below. 🚀
Happy Coding!
Suggested Reading:
- How to Remove .html, .php, or Both from URLs
- How to Buy cPanel Hosting for Your Website: Beginner’s Guide
- How to Install an SSL Certificate in cPanel
- How to Set Up a PostgreSQL Database and User in cPanel
- How to Set Up a MySQL Database & User in cPanel (2 Easy Methods)
- How to Flush DNS Cache on Windows, Mac, Linux & Browsers