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.
Table of Contents
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
publicfolder (except.htaccess) to the root folder. - Open the
index.phpfile 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.confor 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
rootdirective:
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
htmldirectory:
mv html html_backup
- Create a symbolic link from
htmlto Laravel’spublicfolder:
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.
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
FAQs on How to Remove /public from Laravel URL
Why does Laravel include /public in the URL by default?
Laravel includes /public in the URL for security reasons. The public directory is the only folder that should be directly accessible by users because it contains front-facing assets like CSS, JavaScript, and index.php, while keeping the rest of the application (controllers, models, views, configuration) secure outside the web root.
What is the recommended method to remove /public from Laravel URL on shared hosting?
The recommended method for shared hosting is using an .htaccess file. Create or edit the .htaccess file in the root Laravel folder and add rewrite rules to redirect all requests to the public directory, which safely removes /public from the URL without exposing application files.
How can I remove /public from Laravel URL by changing the document root?
You can change the document root to point directly to the public folder. In cPanel, go to Domains > Manage and update the document root to include /public. For Apache or Nginx servers, modify the configuration file to set the document root to the public directory and restart the web server.
Is moving public folder contents to root directory a safe method?
No, moving public folder contents to the root directory is not recommended and poses security risks. This method exposes critical Laravel directories like routes, storage, and config to public access, making your application vulnerable to attacks. It should only be used as a last resort.
What is the symbolic link method for removing /public from URL?
The symbolic link method involves creating a link that points your web server’s document root (like public_html) to Laravel’s public directory. This keeps the Laravel structure intact while making public files accessible without /public in the URL. It’s a clean solution for advanced users with server access.