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
# 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 ^ index.php [L]
</IfModule>
Btw, someone told me not to change the structure of the PHP file
CodePudding user response:
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
<?php
define('LARAVEL_START', microtime(true));
/*
|--------------------------------------------------------------------------
| Register The Auto Loader
|--------------------------------------------------------------------------
|
| Composer provides a convenient, automatically generated class loader for
| our application. We just need to utilize it! We'll simply require it
| into the script here so that we don't have to worry about manual
| loading any of our classes later on. It feels great to relax.
|
*/
require __DIR__.'/../laravel/vendor/autoload.php';
/*
|--------------------------------------------------------------------------
| Turn On The Lights
|--------------------------------------------------------------------------
|
| We need to illuminate PHP development, so let us turn on the lights.
| This bootstraps the framework and gets it ready for use, then it
| will load up this application so that we can run it and send
| the responses back to the browser and delight our users.
|
*/
$app = require_once __DIR__.'/../laravel/bootstrap/app.php';
$kernel = $app->make(Illuminate\Contracts\Http\Kernel::class);
$response = $kernel->handle(
$request = Illuminate\Http\Request::capture()
);
$response->send();
$kernel->terminate($request, $response);
And in your .htaccess
file you need to add this lines of code
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</IfModule>
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 (. ) /public/$1 [L]
# Send Requests To Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ /public/index.php [L]
</IfModule>
CodePudding user response:
How to deploy a Laravel project?
- Copy all the contents of the
/your_laravel_project/public/
inpublic_html
- Copy the other files and folders from
/your_laravel_project
to a directory which is beforepublic_html
, let's call it/core
- in
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';