I’m trying to deploy my laravel project on shared hosting Cpanel, I divide my laravel project into 2 folders (i do this so the user can’t check out my .env file):
- laravel (all laravel files except public)
- public (I moved all the contents of the file to public_html)
I put both of them to public_html, and I wonder what I have to do with htaccess file to point it to the public folder?
My htaccess file
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</IfModule>
RewriteEngine On
RewriteRule ^(.*)$ public/$1 [L]
# 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]
# Send Requests To Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ public/ [L]
</IfModule>
Btw, someone told me not to change the structure of the PHP file
2
Answers
You need to Edit the
index.php and .htaccess
file in your public folder.Why you need this changes because of now your files are outside of
Laravel
Project folder and for mapping the project you need to give a new path to this files.You only need to change code in your
public/index.php
And in your
.htaccess
file you need to add this lines of codeHow to deploy a Laravel project?
/your_laravel_project/public/
inpublic_html
/your_laravel_project
to a directory which is beforepublic_html
, let’s call it/core
public_html
, edit the routes inindex.php
file.I’m not sure about the older versions but in Laravel 8.x and 9.x you only need to edit 3 lines in
index.php
file:if (file_exists($maintenance = __DIR__.'/../storage/framework/maintenance.php')) {...
toif (file_exists($maintenance = __DIR__.'/../core/storage/framework/maintenance.php')) {
require __DIR__.'/../vendor/autoload.php';
torequire __DIR__.'/../core/vendor/autoload.php';
$app = require_once __DIR__.'/../bootstrap/app.php';
must change to$app = require_once __DIR__.'/../core/bootstrap/app.php';