skip to Main Content

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):

  1. laravel (all laravel files except public)
  2. 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


  1. 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(IlluminateContractsHttpKernel::class);
    $response = $kernel->handle(
        $request = IlluminateHttpRequest::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>
    
    Login or Signup to reply.
  2. How to deploy a Laravel project?

    1. Copy all the contents of the /your_laravel_project/public/ in public_html
    2. Copy the other files and folders from /your_laravel_project to a directory which is before public_html, let’s call it /core
    3. in public_html, edit the routes in index.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:

    1. if (file_exists($maintenance = __DIR__.'/../storage/framework/maintenance.php')) {... to if (file_exists($maintenance = __DIR__.'/../core/storage/framework/maintenance.php')) {
    2. require __DIR__.'/../vendor/autoload.php'; to require __DIR__.'/../core/vendor/autoload.php';
    3. $app = require_once __DIR__.'/../bootstrap/app.php'; must change to $app = require_once __DIR__.'/../core/bootstrap/app.php';
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search