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.
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:
httpd.conf
or apache2.conf
. If you are using Nginx, the configuration file is typically located at /etc/nginx/sites-available/default.
<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 } }
/path/to/your/laravel
with the actual path to your Laravel project.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.
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.
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.
.htaccess
..htaccess
file:<IfModule mod_rewrite.c> RewriteEngine On RewriteRule ^(.*)$ public/$1 [L] </IfModule>
Next, you need to update Laravel’s configuration to recognize that the application is being served from a subdirectory.
server.php
file.require __DIR__.'/public/index.php';
Replace it with the following:
require __DIR__.'/public/../index.php';
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.
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:
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.
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.
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!
Today we are going to learn about managing multiple PHP versions on ubuntu with xampp.…
Let's understand about how to use coding to improve your website's SEO. In today’s computerized…
Let's understand the most important linux commands for web developers. Linux, as an open-source and…
Today we are going to discuss top 75+ Laravel interview questions asked by top MNCs.Laravel,…
Today we will discuss about the Mailtrap integration with laravel 10 .Sending and receiving emails…
Today we are going to integrate FCM (Firebase Cloud Messaging) push notifications with ionic application.Firebase…