Share this:

Today we are going to learn How to remove /public/ from a Laravel 10 URL. Laravel, one of the most popular PHP frameworks, provides developers with a robust and efficient platform for building web applications.

With the release of Laravel 8, the framework introduced a simplified directory structure that removed the need for the /public folder in the URL.

However, if you’re working with an older version like Laravel 10, you may still have URLs that contain /public/.

In this blog post, we’ll explore how you can remove /public/ from a Laravel 10 URL and create cleaner, more user-friendly links for your application.

Also Read: How do you check “if not null” with Eloquent? Laravel

Understanding the issue:

By default, when you access a Laravel 10 application, the web server’s document root should point to the /public directory.

This ensures that only the public directory’s contents are accessible from the outside, providing a security measure to safeguard sensitive files and directories.

However, this can result in URLs that contain /public/, making them less aesthetically pleasing and harder to remember.

Solution:

Update your web server configuration:

To remove /public/ from your Laravel 10 URL, you need to configure your web server to point directly to the /public directory. This can be done by modifying the web server configuration file. Here are instructions for commonly used web servers:

  • Apache: If you’re using Apache, locate the virtual host configuration file for your Laravel application. Look for the DocumentRoot directive and change it to point directly to the /public directory. After saving the changes, restart the Apache service.
  • Nginx: For Nginx users, open the configuration file for your Laravel application. Find the root directive within the server block and update it to point to the /public directory. Save the changes and restart the Nginx service.

Also Read: Error “Target class controller does not exist” when using Laravel 10

Configure the .htaccess file (for Apache users only):

If you’re using Apache, you’ll also need to update the .htaccess file located in the /public directory. Open the .htaccess file and look for the following lines:

RewriteEngine On 
RewriteRule ^(.*)$ public/$1 [L] 

Comment out or remove these lines, as they are responsible for redirecting requests to the /public directory. Save the changes and ensure that the updated .htaccess file is in the correct location.

Clear the configuration cache:

After making changes to the web server configuration and .htaccess file, it’s essential to clear Laravel’s configuration cache to ensure that the modifications take effect.

Run the following command from the terminal within your Laravel application’s root directory:

php artisan config:clear 

This will clear the configuration cache, allowing Laravel to pick up the updated settings.

Also Read: How to Query All GraphQL Type Fields Without Writing a Long Query in Laravel?

Test your application:

With the necessary configuration changes made, you can now test your Laravel application. Access your application via the domain or URL without the /public/ segment.

If everything was set up correctly, you should be able to navigate your application without seeing /public/ in the URL.

Conclusion:

Removing /public/ from a Laravel 10 URL is a relatively simple process that involves updating your web server configuration and modifying the .htaccess file if you’re using Apache.

By following the steps outlined in this blog post, you can create cleaner and more user-friendly URLs for your Laravel 10 application.

Remember to take the necessary precautions when modifying server configurations and keep backups of any files you modify to ensure a smooth transition.

Share this:

Categorized in: