How to Remove Public From Laravel URL (4 Easy Methods)

Remove Public From Laravel URL

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.

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:

  1. Move everything inside the public folder (except .htaccess) to the root folder.
  2. 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

  1. Log into cPanel and go to Domains > Manage.
  2. Locate your domain and find the Document Root section.
  3. Change the document root from /home/youruser/public_html/ to /home/youruser/public_html/public.
  4. Save the changes and restart Apache if necessary.

For Apache Users (VPS/Dedicated Server)

  1. Open your Apache configuration file (httpd.conf or your virtual host file).
  2. Find the <VirtualHost> block for your domain and update the DocumentRoot:
DocumentRoot "/var/www/html/public"
<Directory "/var/www/html/public">
    AllowOverride All
    Require all granted
</Directory>
  1. Save the file and restart Apache:
sudo systemctl restart apache2

For Nginx Users

  1. Open your Nginx configuration file:
sudo nano /etc/nginx/sites-available/default
  1. Change the root directive:
root /var/www/html/public;
  1. 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

  1. Backup your current html directory:
mv html html_backup
  1. Create a symbolic link from html to Laravel’s public folder:
ln -s /home/youruser/laravel_project/public /home/youruser/html
  1. 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

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:

Picture of Kennedy Dzigbenyo

Kennedy Dzigbenyo

Kennedy Dzigbenyo is a dedicated Software Engineer and WordPress Expert whose expertise lies in turning conceptual digital dreams into tangible realities. He excels in the art of creating digital solutions that respond to contemporary needs.

Leave a Reply

Enjoy 20% Off Your First Order!

Use promo code WELCOME at checkout to claim your exclusive discount.

Get 20% OFF on your first order

Oh hi there! It’s nice to meet you.

Please Kindly, sign up to receive awesome content in your inbox, every month.