Table of contents
![Removing the public path in a route [→]](../images/public-removal.webp)
The Laravel framework has an index.php file within its public directory. This serves as the entry point for all incoming traffic/requests.
This means the only way to access any route is to go through the /public directory.
Thus to reach /home, you would have to visit domain.tld/public/home. In the rest of this article, we will look at how to get rid of the /public sub-path from our URLs using the .htaccess config file.
Note: The discussed solution only works on systems running an Apache [↗] or Lightspeed [↗] web server [→]
Hypertext Access Configuration
There are different ways to remove the /public path from your URLs, this post will however focus on a solution using the .htaccess configuration file.
Hypertext Access or .htaccess is a configuration file that allows you to override the default behavior of an Apache webserver [↗].
In this case, we will be using it to rewrite how our web server handles routing.
- Go ahead and create a
.htaccessfile in the root folder of your Laravel app. I.E:laravel-app/.htaccess
Directory Structure
01: laravel-app
02: ├── .htaccess- Next copy and paste the configuration below into the the newly created file.
.htaccess
01: RewriteEngine on
02: RewriteCond %{HTTP_HOST} ^domain.tld$ [NC,OR]
03: RewriteCond %{HTTP_HOST} ^www.domain.tld$
04: RewriteCond %{REQUEST_URI} !public/
05: RewriteRule (.*) /public/$1 [L] Code walkthrough
Line 01[→]: By setting RewriteEngine on we ensure that the Apache mod_rewrite module is enabled and ready to rewrite URLs.Line 02[→]: Checks if the provided hostname matches "domain.tld"Line 03[→]: Same as above but starting with wwwLine 04[→]: Check if the requested URI does not start with "public/"Line 05[→]: If all conditions are met, rewrite the URL to prepend "/public/" to the request URI
Setting .htaccess on Hostinger
![Hostinger Dashboard Image [→]](../images/hostinger-dashboard.png)
- Once you are logged onto your Hostinger account, navigate to the website you would like to configure.
- From here click on the File Manager card.
![Hostinger File Manager [→]](../images/file-manager.png)
- Once you land on the file manager, navigate to public_html > public
- click on the "New file" option on the left-hand navigation.
- Create a file with the name ".htaccess"
- Click on Create to save this file
- Double-click on the file i.e:
.htaccessto open it - Fill it with the configuration from this section [→]
- Click on the save icon on the far right to save the file
That should do the trick.
Here is another article you might like 😊 Converting Bootstrap 5 Styles To Tailwind (WIP)