How to Remove Public from URL in Laravel 10?

In Laravel, removing the "public" from the URL is a common practice to make your application's URLs cleaner and more user-friendly. Starting from Laravel 10, the framework provides a built-in way to achieve this using the public directory as the document root.

However, as of my knowledge cutoff in September 2021, Laravel 10 has not been released. Laravel 10 was the latest version available at that time. It's important to note that information provided beyond my knowledge cutoff may not be accurate. Please refer to the official Laravel documentation or reliable sources for the most up-to-date information on Laravel versions beyond Laravel 10.

That being said, in Laravel 10 or earlier versions, you can achieve the removal of "public" from the URL by following these steps

Step 1

Ensure that your web server (such as Apache or Nginx) is correctly configured to point to the public directory of your Laravel application as the document root. This step is crucial for the clean URL setup.

Step 2

Once your web server is correctly configured, you need to update the .htaccess file located in your Laravel project's root directory. If you don't see the .htaccess file, it might be hidden, so make sure you have enabled the option to view hidden files in your file explorer.

Step 3 

Open the .htaccess file and edit for the following lines:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_URI} !^/public/
    RewriteRule ^(.*)$ public/$1 [L]
</IfModule>

This code ensures that if the URL doesn't already contain "/public/", it will be internally redirected to the public directory.

After completing these steps, you should be able to access your Laravel application without the "public" segment in the URL. For example, instead of http://your-domain.com/public/index.php, you can access your application using http://your-domain.com/index.php or even http://your-domain.com/.

Remember to adjust the configuration of your web server accordingly if you are using a different server or a non-standard configuration. Additionally, always make sure to back up your files before making any modifications.