I have been getting a lot of queries regarding, how to remove public url from laravel application so that we can host it in cPanel or shared hosting servers.
As of now laravel can be hosted itself in their dedicated VPS servers and is expensive but most of them are still using hostgator (baby plan hosting) and other shared hosting to host their application, since they are cheap and easy to use.
In this tutorial we are going to learn how we can remove public url from the laravel application and host it in shared cPanel / Hosting.
We have two different approaches for removing public URLĀ from the laravel application :-
First Approach :-
- Go to your laravel application and go inside
publicfolder. - Move your
.htaccessfile 1 step outside of the public folder, (which will be your laravel application root directory). - Rename your
server.phptoindex.phpfrom the laravelroot directory. - Done.
2nd Approach :-
This approach is not straight forward but will help to secure your laravel application in much better way. Lets get started.
Step 1:-
Create new folder named local and copy all the files and folders of your laravel application directory into another folder except the public folder.
Step 2 :-
Now move all the files and folders of public folder and paste it in root directory. Look at below image how your files should look by now. Your public folder should be empty by now, get rid of it to avoid confusion.
Root Directory

Local Directory

Step 3 :-
Open up your index.php file which is now on root directory of your laravel application. You have to edit the index.php file which was previously on inside public folder and edit these lines.
|
1 |
require __DIR__.'/../bootstrap/autoload.php'; |
to
|
1 |
require __DIR__.'/local/bootstrap/autoload.php'; |
And
|
1 |
$app = require_once __DIR__.'/../bootstrap/app.php'; |
to
|
1 |
$app = require_once __DIR__.'/local/bootstrap/app.php'; |
Done.
Now try visiting your browser and navigate to url.
If you want to upload your website into shared hosting, directly upload it in your public_html directory inside cPanel.
If you have any problem, feel free to mention them in your comments.


Leave a Reply