Today, we are going to learn How to Remove “public” from Laravel 9 URL with htaccess.In recent years, Laravel has emerged as one of the most popular PHP frameworks due to its elegant syntax, robust features, and developer-friendly environment. However, one common issue faced by Laravel developers is the presence of the “public” keyword in the URL. This article aims to guide you on how to remove the “public” from the URL using an htaccess file, providing a cleaner and more user-friendly URL structure.
Introduction
When you install a fresh Laravel 9 application, the default URL structure includes the “public” keyword in the URL. For example, accessing a page would typically involve a URL like “http://example.com/public/about.” This can be cumbersome and unappealing from a user’s perspective. To overcome this issue, we can leverage the power of htaccess to remove the “public” keyword from the URL and present a cleaner URL structure to the users.
Creating the htaccess File
To begin, create a new file in the root directory of your Laravel 9 application and name it “.htaccess” (note the dot in the filename). Ensure that the file is created in the same directory as the “public” folder.
Configuring the htaccess File
Open the “.htaccess” file in a text editor and add the following lines of code:
Options -MultiViews -Indexes
RewriteEngine On
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_URI} !(\.css|\.js|\.png|\.jpg|\.gif|robots\.txt)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/public/
RewriteRule ^(css|js|images)/(.*)$ public/$1/$2 [L,NC]
The above code snippet configures the htaccess file to redirect all requests to the “public” folder while hiding the “public” keyword from the URL.
Testing the Modified URL Structure
Save the htaccess file and navigate to your Laravel application in a web browser. You should notice that the “public” keyword is no longer present in the URL. For example, the previous URL “http://example.com/public/about” would now appear as “http://example.com/about.”
Benefits of Removing “public” from URL
By removing the “public” keyword from the URL, you can provide a cleaner and more professional-looking URL structure to your users. This improves the overall user experience and makes your Laravel application more appealing. Additionally, it also helps in search engine optimization (SEO) by presenting concise and keyword-rich URLs.
Conclusion
In this article, we explored the process of “How to Remove “public” from Laravel 9 URL with htaccess”. By following the steps outlined, you can enhance the user experience of your Laravel application and create SEO-friendly URLs. Remember to update all the necessary routes and links within your application to align with the modified URL