How to Remove Public from URL in Laravel 10? (Examples + Source code)
So, if you are a laravel beginner then you might be encounter with this issue and want to remove public from the URL. By default, when you set up a new Laravel project and run it locally, you’ll notice that the URLs contain a /public
segment, like http://localhost/my-laravel-app/public/
. However, in a production or development environment, you would want to remove the /public
segment from the URLs to achieve cleaner and more professional-looking URLs.
In this article, we will explore the steps to remove /public
from the URL in a Laravel 10 application.
Method 1: Using Virtual Hosts to remove public from URL in laravel
The most recommended approach to remove /public
from your Laravel URL is by using virtual hosts. Virtual hosts allow you to set up separate hostnames for your applications, making your URLs cleaner and more user-friendly. Here’s a step-by-step guide to achieve this:
Step 1: Create a Virtual Host
- Open your server configuration file. For example, if you are using Apache, it might be
httpd.conf
orapache2.conf
. If you are using Nginx, the configuration file is typically located at/etc/nginx/sites-available/default.
- Add a new virtual host entry. For Apache, it will look something like this:
<VirtualHost *:80> ServerName my-laravel-app.local DocumentRoot /path/to/your/laravel/public </VirtualHost>
For Nginx, the configuration will be:
server { listen 80; server_name my-laravel-app.local; root /path/to/your/laravel/public; index index.php; location / { try_files $uri $uri/ /index.php?$query_string; } location ~ \.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/run/php/php7.4-fpm.sock; # Adjust this to your PHP version } }
- Make sure to replace
/path/to/your/laravel
with the actual path to your Laravel project. - Save the configuration file and restart your web server to apply the changes.
Step 2: Update the Hosts File
Next, you need to add an entry in your computer’s hosts file to map the virtual host to your localhost. The hosts file is usually located at C:\Windows\System32\drivers\etc\hosts on Windows
or /etc/hosts
on macOS/Linux.
Add the following line at the end of the hosts file:
127.0.0.1 my-laravel-app.local
Again, replace my-laravel-app.local
with the ServerName you used in your virtual host configuration.
Step 3: Access Your Laravel Application
Now, you should be able to access your Laravel application without the /public
segment in the URL. Open your web browser and enter http://my-laravel-app.local
. Your Laravel application should load without any issues.
Method 2: Using .htaccess and remove public from the URL in laravel
An alternative method to remove /public
from the Laravel URL is by using an .htaccess
file. However, this method is not recommended for production environments as it may cause issues with certain configurations and is less efficient than using virtual hosts.
Step 1: Create the .htaccess File
- In the root directory of your Laravel project, create a new file named
.htaccess
. - Add the following content to the
.htaccess
file:
<IfModule mod_rewrite.c> RewriteEngine On RewriteRule ^(.*)$ public/$1 [L] </IfModule>
Step 2: Update Laravel Configuration
Next, you need to update Laravel’s configuration to recognize that the application is being served from a subdirectory.
- In the root directory of your Laravel project, open the
server.php
file. - Look for the following line:
require __DIR__.'/public/index.php';
Replace it with the following:
require __DIR__.'/public/../index.php';
Step 3: Test the Application
Now, you should be able to access your Laravel application without the /public
segment in the URL. Open your web browser and enter http://localhost/my-laravel-app
. Your Laravel application should load correctly. You can also clear config cache before test your application.
Method 3: Change the Domain root directory, it will remove the public from URL in laravel
On the live server, you can set the domain’s root directory directly to the public directory of your Laravel project. This way, you don’t need to explicitly include /public in the URL, and your application will be accessible without it.
Let’s walk through the steps to achieve this:
- Upload Your Laravel Project: First, upload your entire Laravel project (including the public folder) to your live server. You can use FTP, SFTP, or any other method your hosting provider supports.
- Configure Domain Root: Log in to your hosting control panel or domain registrar’s dashboard. Look for the settings related to your domain and web hosting configuration.
- Set Document Root: In the domain or web hosting settings, find the option to set the document root directory. This setting determines which folder on your server will be considered the webroot for your domain. Change the document root to point directly to the public folder of your Laravel project.
- Save Changes: Save the changes you made to the domain settings.
- DNS Propagation: Wait for the DNS changes to propagate. This process might take some time (usually a few hours), but after that, your domain will be correctly pointed to the public folder of your Laravel project.
Once the DNS changes have propagated, you can access your Laravel application using the domain directly without needing to specify /public
in the URL. For example, if your domain is example.com, accessing http://example.com
will directly load your Laravel application.
Setting the domain root to the public directory is the most straightforward and recommended way to remove the /public
segment from the URL in a live server environment. This approach ensures that only the publicly accessible files are exposed, providing better security for your Laravel application.
By following this method, you can have clean and professional-looking URLs for your Laravel application on the live server without the need to modify any Laravel configuration or use .htaccess
tricks.
Method 4: Move index.php and .htaccess from public to root directory
You can indeed remove the /public
segment from the URL by moving the index.php
and .htaccess
files from the /public
directory to the root directory of your Laravel project. This approach is an alternative to setting the domain root directly to the public
directory and achieves the same result.
Here’s a step-by-step guide on how to do it:
1. Backup Files: Before making any changes, it’s always a good idea to create a backup of your project to ensure you can revert in case of any issues.
2. Move index.php
: Move the index.php
file from the /public
directory to the root directory of your Laravel project.
You need to move index.php
from /laravel/public/index.php
to /laravel/index.php
.
3. Move .htaccess
: Similarly, move the .htaccess
file from the /public
directory to the root directory of your Laravel project.
5. Test Your Application: With these changes, your application should now be accessible without the /public
segment in the URL. You can access your Laravel application by entering the domain or project URL directly, like http://example.com
or http://localhost/my-laravel-app
.
By moving the index.php
and .htaccess
files to the root directory, you effectively change the entry point of your application. This approach is commonly used in shared hosting environments where you might not have access to change the domain’s document root.
Conclusion
In this article, we have explored four methods to remove /public
from the URL in a Laravel 10 application. The recommended approach is to use virtual hosts or set the document root, as it provides a cleaner and more efficient solution for production environments. However, if you’re working in a development environment or cannot use virtual hosts for some reason, the .htaccess
method can be used as an alternative.
Remember to choose the method that best fits your project’s requirements and always ensure you follow best practices for security and performance when deploying your Laravel application in production. Happy coding!