
- Views: 1.1K
- Category: Laravel
- Published at: 31 Aug, 2023
- Updated at: 01 Sep, 2023
How to customize url by Remove the public from the URL in Laravel 10
Why Is the Public Being Removed From the URL in Laravel 10?
When you deploy a Laravel application, you may notice that users need to append /public to the base URL to access your application properly. For example, http://yourwebsite.com/public. While this may not seem like a big deal initially, it has multiple drawbacks.
Firstly, there are security concerns with leaving the /public directory visible in the URL. Secondly, it affects the aesthetics and readability of the URL, which may be important for both users and client presentations.
SEO-Friendly URLs
From a Search Engine Optimization (SEO) standpoint, URLs should be as clean, concise, and descriptive as possible. Search engines like Google give weight to URLs that are easy to read and describe the content accurately. Having a '/public' in the URL doesn't contribute to this and may affect your site's SEO negatively.
Now, Let's analyze how to delete or remove the public from your Laravel 10 application URL. We'll be using cPanel as our hosting platform for this example.
Hosting Laravel 10 on cPanel and Removing 'public' from URL
Upload Your Laravel 10 application/Project to cPanel Zip your Laravel project on your local machine. Log in to your cPanel dashboard.
Access the File Manager within your cPanel dashboard and make your way to the folder where you intend to place your Laravel 10 application. Upload the zipped Laravel project and extract it. Now you need to Update the .htaccess file To remove the 'public' portion of the URL from your Laravel project, you must modify the .htaccess file. The updated code for the .htaccess file is displayed below. This version only retains the principles necessary to eliminate 'public' from the URL.
<IfModule mod_rewrite.c>
RewriteEngine On
# If the request URI does not start with /public, prepend /public
RewriteCond %{REQUEST_URI} !^/public/
RewriteRule ^(.*)([^/])$ /public/$1$2 [L]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
Launch the cPanel File Manager and go to the location of your .htaccess file. Replace the existing code with the above code. Save and close the file.
Now, when you visit your website, the 'public' part should be removed from the URL, leaving you with a clean, SEO-friendly, and secure URL.
And there you have it! With just a few steps, you've successfully removed 'public' from your Laravel 10 application's URL using cPanel. This not only makes your URLs look cleaner but also enhances the SEO performance of your website.
Some developers opt to move all the files from the public folder to the root directory of their Laravel 10 application.
Certainly! Moving all files from the public folder to the root directory of a Laravel 10 application may seem like a quick way to remove /public from URLs, but it's not recommended for several key reasons:
Security Risks
This exposes sensitive files like .env, which can contain database credentials, to potential public access.
Code Integrity
Merging public and private files can blur boundaries, leading to errors and complicating future updates.
Server Overhead
Placing all files in the root could impact web server performance, as the server has to sift through more files for each request.
Community Support
Deviating from Laravel's standard structure can make troubleshooting and community support challenging.
For these reasons, it's better to use more secure methods to remove the /public URL segment, like adjusting web server settings or using .htaccess configurations.
Conclusion: Crafting a Polished Laravel Experience
Navigating the complexities of web development often presents challenges that are not immediately apparent. The inclusion of '/public' in your Laravel 10 application URL is one such hurdle. It may appear harmless, but as we've seen, it can have ramifications ranging from aesthetic inconveniences to SEO impairments and even potential security vulnerabilities.
By following the steps outlined in this blog, you've not only enhanced the user experience but also fortified the security of your application. The simplified, SEO-friendly URLs are more appealing to search engines, thereby increasing your chances for higher rankings. Through cPanel's user-friendly interface, the task of URL modification becomes a breeze, making this an essential tweak for any Laravel 10 project.
In the ever-evolving world of web development, it's these small optimizations that accumulate to form a well-rounded, secure, and efficient application. So take pride in making this modification; it's a significant step towards achieving digital excellence.
- Tag
- Public
- Laravel 10
0 Comment(s)