When deploying a Laravel software on shared internet hosting, you could encounter the widespread difficulty of the “public” folder showing in your web site’s URL. This will make your URLs much less elegant and user-friendly. On this tutorial, we’ll discover two efficient options to take away “public” from the URL in Laravel 10.
Earlier than we dive into the options, it’s important to grasp why we have to take away “public” from the Laravel URL. If you add a Laravel software to shared internet hosting, you typically can’t set the trail on to the
In Laravel, the
This
To additional clear up your Laravel URLs, comply with these steps:
These steps successfully route all incoming requests to the
Eradicating “public” out of your Laravel 10 software’s URL is a vital step to make your web site URLs extra user-friendly. You may select between utilizing a
Why Take away ‘Public’ from Laravel URL?
Earlier than we dive into the options, it’s important to grasp why we have to take away “public” from the Laravel URL. If you add a Laravel software to shared internet hosting, you typically can’t set the trail on to the
index.php
file. Consequently, it’s essential to run the applying from the “public” folder, which may result in URLs like yourwebsite.com/public/index.php
. That is removed from best, and we’ll present you methods to clear it up.Resolution 1: Eradicating ‘Public’ Utilizing .htaccess
In Laravel, the
.htaccess
file performs an important function in URL rewriting and can be utilized to get rid of “public” out of your URLs. Observe these steps:- Create a New
.htaccess
File: Go to the foundation folder of your Laravel software and create a brand new.htaccess
file. - Edit
.htaccess
: Open the newly created.htaccess
file and add the next code:
Code:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
- Save and Add: Save the modifications and add the
.htaccess
file to the foundation listing of your Laravel software.
This
.htaccess
code makes use of Apache’s mod_rewrite
module to redirect all requests to the “public” folder, successfully eradicating “public” from the URL.Resolution 2: Rename server.php and Transfer .htaccess
To additional clear up your Laravel URLs, comply with these steps:
- Rename
server.php
: In your Laravel root folder, rename theserver.php
file toindex.php
. - Transfer
.htaccess
: Find the.htaccess
file throughout the/public
listing of your Laravel software. Copy it and paste it into your Laravel root folder. - Transfer Property: In case your software has CSS and JS recordsdata within the “public” folder, transfer them to the foundation folder as effectively. You’ll want to replace the references to those property in your views and layouts.
These steps successfully route all incoming requests to the
index.php
file and streamline your URLs.Conclusion
Eradicating “public” out of your Laravel 10 software’s URL is a vital step to make your web site URLs extra user-friendly. You may select between utilizing a
.htaccess
file or renaming recordsdata and shifting the .htaccess
for attaining this objective. By following these options, your Laravel software may have cleaner, extra professional-looking URLs, enhancing person expertise and search engine marketing rankings.