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