Reputation: 1839
Even though there are hundreds similar questions out there, but none is actually solving my problem. I have setup a Laravel 7.14.1 application, and I uploaded it to a shared host.
To make it work on shared host, I did the following steps:
1- Renamed server.php to index.php
2- Moved .htaccess to root
3- In .htaccess I put the following rules
RewriteEngine On
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_URI} !(\.css|\.js|\.png|\.jpg|\.gif|robots\.txt)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/public/
RewriteRule ^(css|js|images)/(.*)$ public/$1/$2 [L,NC]
4- Created a test route like follows:
Route::get('/test', function () {
return 'HI';
});
Now when I visit domain.com/test
it gives me 404 error
But when I visit doamin.com/public/test
it works
How can I remove the /public part from my url?
Upvotes: -1
Views: 1554
Reputation: 3900
Easy way to resolve this.
Let me know how it goes
Upvotes: 1
Reputation: 342
The simplest and safest solution for shared hostings:
mv .htaccess .htaccess-BACKUP
to backup your current .htaccess
cp public/index.php ./ && cp public/.htaccess ./
NOTE: If you don't have SSH access, you can do exactly the same thing manually from cPanel > File Manager
Upvotes: 0
Reputation: 158
The following are the steps you can follow to host your laravel application on shared hosting(cpanel)
1 Step: Login to your shared hosting account (cpanel) dashboard then find File Manager icon then click it
Click public_html , actually this is where normally we upload our web application contents, so upload your laravel project here but we need to do extract work to make our project running please proceed with following steps
2. Step: Organize your laravel project
Create folder in main root of your laravel project any name will fit, mine i call it system as shown below
Then i move all the other folders except public to this new created folder.
Here you will result into
Takes all your public content outside the main root as shown below
Then here what will result
You can remove public folder or leave it
3 Step: Edit index.php file found in your main root
We need to edit line code number 22 and 36 to the following as shown below
NB: The secret behind is to tell our index.php file where it can find those two files
You can test your project by navigating to your domain.
4. Step : Conclusion,
There is interesting package that sum up all what we did as laravel package please have a look, click here
Upvotes: 0