If you don’t know how to deploy laravel 8 or 9 project on cpanel then read this article and learn from scratch How to deploy 8 or 9 laravel project on godaddy cpanel shared hosting.
Step by Step Process to Deploy Laravel 8 or 9 Project on Cpanel
Step 1 – Select all folders and files and make a zip file by compressing with any name
Step 2 – Login to your cPanel interface (domainname.com/cpanel)
Step 3 – Open File Manager under the Files section
Step 4 – Open the public_html folder
Step 5 – Click on the Upload Option and upload the zip file then reload this folder by clicking the reload button.
Step 6 – Extract the uploaded zip file in the public_html folder
Step 7 – Hit the Domain and your laravel project shows a directory structure just like below shared image.
Your laravel project is running at /public folder. You can check by hitting the /public folder. Now what we do If laravel project shows a directory structure on root at cPanel shared hosting.
So the question is, why did this happen when we have done everything correctly? and what is the solution of this.
Why Laravel Project Showing Directory Structure on cPanel Hosting
As per my understanding it happened because your server’s root directory is public_html, so server requests does not found index.php file at root because it is placed in the public folder. So your laravel project shows directory structure at root and running at /public folder.
So the solution is, make “/public” folder as our root folder in server configuration. Isn’t it sounds good and easy? But most developers and newbie developers don’t know server configuration, then what to do? So here are two easy solutions for How to run laravel project on root domain instead of public folder.
How to Run Laravel Project on Root Directory not from /public Folder
If you have already googled this issue you will find one more solution which in complete not recommended by me. According to that not recommended solution you need to move your all files from /public folder to your root folder and made a few changes in index.php file and server.php file.
The reason behind to say, this is the not a recommended solution.
So here are two easy solutions for How to run laravel 8 or 9 project without moving index file to the root directory.
Solution 1 – .htaccess way
Follow two following steps to run laravel project without /public in url
Step 1 – Move your .htaccess file from /public folder to root folder(public_html)
Step 2 – Edit the .htaccess file and place the following code after “RewriteEngine On” and hit the domain laravel project is now running at the root domain.
RewriteEngine On
# Redirect all requests /public to root folder
RewriteCond %{REQUEST_URI} !/public
RewriteRule ^(.*)$ public/$1 [L]
# Prevent direct access to the /public folder
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /public/
RewriteRule ^public/(.*) /$1 [R=302,L]
Solution 2 – Change the Path
Step 1 – Create a seperate directory out side the public_html folder. For example let’s consider the name “laravelapp”.
Step 2 – Move all your FIles and Folder from “public_html” to “laravelapp” except the public folder.
Step 3 – Move all files and folders from /public_html/public direcroty to /public_html direcroty.
Step 4 – Edit index.php file and change the path and save the file.
require __DIR__.'/../vendor/autoload.php';
to
require __DIR__.'/../laravelapp/vendor/autoload.php';
$app = require_once __DIR__.'/../bootstrap/app.php';
to
$app = require_once __DIR__.'/../laravelapp/bootstrap/app.php';
Hit the domain and your laravel project is now running at the root domain.
Please let us know if this solution works for you or not in the comments.