Wednesday, February 12, 2025
Google search engine
HomeCommunitiesHow to Remove 'Public' from URL in a Laravel Project Deployed on...

How to Remove ‘Public’ from URL in a Laravel Project Deployed on a Subdomain

When deploying a Laravel project on a subdomain (e.g., on Hostinger), you might encounter an issue where accessing your website without adding /public in the URL results in a 404 error. This issue arises because Laravel’s default setup requires the public directory to be part of the URL. Here’s how to remove ‘public’ from the URL and access your Laravel site directly:

Step-by-Step Solution

  1. Create a .htaccess File in the Root Directory:
    • In the root directory of your Laravel project. create a new file named .htaccess.
  2. Add the Following Code to the .htaccess File:
    • Open the newly created .htaccess file in a text editor and paste the following code:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/public/
RewriteRule ^(.*)$ /public/$1 [L,QSA]
  1. Save the .htaccess File:
    • Save the changes to the .htaccess file and close the editor.
  2. Test Your Website:
    • Now, access your website using the subdomain URL without appending /public. It should work correctly without any 404 errors.

Conclusion

By following these steps, you can ensure that your Laravel project runs smoothly without needing to add /public to the URL, providing a cleaner and more user-friendly URL structure for your website.

Developer: Amjad Hussain
Designation: Laravel Developer
LinkedIn: Amjad Hussain
GitHub: AmjadSethar

Find for More Code Error related news Click here.

Subscribe our WhatsApp Channel to received Personalised updates.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments