How to change Laravel public folder to public_html?

Laravel is a popular PHP framework for building web applications. By default, Laravel stores its public files in a directory named “public” at the root of your project. However, some hosting providers use a different naming convention for the public folder, such as “public_html”.

In this post, we will explore how to change Laravel public folder to public_html.

Change Laravel Public Folder Name to public_html

First of all, you need to change your public folder name to public_html. It can be done by using the hosting provider’s file manager or by SSH access. If you are using shared hosting then in most cases, you don’t have ssh access but in C-panel they have a file manager which can be used to rename folders.

Here, you can directly upload the project’s public file to the public_html directory.

Modify Laravel Server File

Laravel blocks access directly outside public directories because of security reasons. The standard flow of the Laravel application is whenever a request comes to a server it will call the server.php file and then load public/index.php.

Let’s modify the server.php file. However, in Laravel 10 server.php file is removed.

<?php

/**
 * Laravel - A PHP Framework For Web Artisans
 *
 * @package  Laravel
 * @author   Taylor Otwell <taylor@laravel.com>
 */

$uri = urldecode(
    parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH)
);

// This file allows us to emulate Apache's "mod_rewrite" functionality from the
// built-in PHP web server. This provides a convenient way to test a Laravel
// application without having installed a "real" web server software here.
if ($uri !== '/' && file_exists(__DIR__.'/public_html'.$uri)) {
    return false;
}

require_once __DIR__.'/public_html/index.php';

Configure App Service Provider

All the core configurations of Laravel applications are in Service providers. In the AppServiceProvider.php file, we have to register a new public path. Modify the register function with the following code.

public function register()
{
    //Public folder name changed with public_html
    $this->app->bind('path.public', function(){
        return base_path().'/public_html';
    });
}

Now, our public path has changed to public_html.

Setting Storage System

At last, we have to configure the file storage link in config/filesystems.php file. Without this configuration, it will be impossible to store or retrieve files for an application.

'links' => [
    base_path('public_html/storage') => storage_path('app/public'),
],

After the above change, we need to link storage using the below command:

php artisan storage:link

It will create storage links by linking public HTML with the storage directory.

Asset Compiling Changes

In this step, we will change the path in webpack.mix.js. It will automatically handle new path while compiling assets like CSS, JS, or SASS.

const mix = require('laravel-mix');

mix.config.publicPath = 'public_html';
mix.js('resources/js/app.js', 'public_html/js')
    .postCss('resources/css/app.css', 'public_html/css', [
        //
]);

Conclusion

In this post, we have learned how to change Laravel public folder to public_html. By following this article, you can easily configure your Laravel application to work with hosting providers that use the public_html naming convention. Remember to always make a backup of your files before making any changes and test your application thoroughly after making any updates.