How to Change Laravel's Public Folder to public_html

Rename Laravel's public folder to public_html with one usePublicPath() call, then fix storage:link and Vite. Written against Laravel 12.

Mahzaib MirzaMahzaib MirzaJune 15, 20223 min read2 comments
How to Change Laravel's Public Folder to public_html

To make Laravel use public_html instead of public, do two things: rename the folder, then tell the framework with $app->usePublicPath() in bootstrap/app.php. If you build assets with Vite, also set publicDirectory in vite.config.js. Nothing else in public/index.php needs to change.

$app = Application::configure(basePath: dirname(__DIR__))
    // ...routing, middleware, exceptions...
    ->create();

$app->usePublicPath(dirname(__DIR__).'/public_html');

return $app;

This post is written against Laravel 12. I checked the steps against the Laravel 12 documentation and framework source rather than running them on a live host, and Laravel 13 is now out, so skim its docs if you are on the newest version.

Do you really need to do this?

Often not. Laravel's deployment docs say your web server should send every request to public/index.php, and the usual fix is to point the domain's document root at public. Changing the folder name is only worth it when you cannot change the document root. Hostinger, for example, says the root folder cannot be changed on its Web, WordPress and Cloud plans, so there it is public_html or nothing.

Whatever you do, do not copy the whole project into the web root. Laravel's docs warn that serving from the project root exposes sensitive configuration files, and .env is one of them.

How Laravel finds its public folder

In the framework source, Application::publicPath() returns the path you set with usePublicPath(), or the base path plus /public if you never set one. The public_path() helper only calls that method, and usePublicPath() also updates the path.public entry in the container. So one call at boot changes every place that asks for the public path.

Step 1: rename the folder

Rename public to public_html. Keep it where it was, next to app, bootstrap and vendor:

my-app/
  app/
  bootstrap/
  public_html/      <- was public/
    index.php
  storage/
  vendor/

The default index.php loads ../vendor/autoload.php, ../bootstrap/app.php and ../storage/framework/maintenance.php, all relative to its own folder. Because public_html sits in exactly the place public did, those paths stay correct and you do not edit the file.

Step 2: call usePublicPath()

The fluent chain in bootstrap/app.php has no method for this, so call it on the application object it returns. Here is the full file:

<?php

use Illuminate\Foundation\Application;
use Illuminate\Foundation\Configuration\Exceptions;
use Illuminate\Foundation\Configuration\Middleware;

$app = Application::configure(basePath: dirname(__DIR__))
    ->withRouting(
        web: __DIR__.'/../routes/web.php',
        commands: __DIR__.'/../routes/console.php',
        health: '/up',
    )
    ->withMiddleware(function (Middleware $middleware): void {
        //
    })
    ->withExceptions(function (Exceptions $exceptions): void {
        //
    })->create();

$app->usePublicPath(dirname(__DIR__).'/public_html');

return $app;

This runs when the application object is created, before the configuration files are loaded, so config that calls public_path() sees the new location.

Step 3: fix the storage link

The default config/filesystems.php builds its links with public_path('storage'), and php artisan storage:link reads that list. After the change, the link is created at public_html/storage:

php artisan storage:link

If something already exists at that path, the command stops with an "already exists" error. Add --force to recreate an existing symlink. It will not replace a real folder or file.

Step 4: tell Vite

The Laravel Vite plugin's publicDirectory option defaults to public, and the build goes to public/build. Set the option so assets and the hot file land in the new folder:

import { defineConfig } from 'vite';
import laravel from 'laravel-vite-plugin';

export default defineConfig({
    plugins: [
        laravel({
            input: ['resources/css/app.css', 'resources/js/app.js'],
            publicDirectory: 'public_html',
            refresh: true,
        }),
    ],
});

Run npm run build and the manifest and compiled files appear in public_html/build. Laravel's docs note that the path given to @vite is relative to the public path, so the Blade side follows usePublicPath() without further changes.

Step 5: update everything else that says public

  • .gitignore: change any entries that mention public/, such as the build folder, the hot file and the storage link, to public_html/.

  • Deploy scripts and CI: any step that copies or syncs public/ needs the new name.

  • Hard-coded paths: search your code for base_path('public'). Use public_path() instead, which follows the setting.

Alternative: keep the app outside public_html

If the web root already exists and you cannot put your project around it, do the reverse: keep the app in its own folder, copy the contents of public into public_html, and edit the three paths in index.php. That layout is walked through step by step in how to deploy Laravel on Hostinger shared hosting.

Which approach should I use?

  • You can set the document root: point it at public and change nothing.

  • The web folder is fixed and your project can sit around it: rename to public_html and use usePublicPath(), as above.

  • The web folder is fixed and already contains other sites: keep the app in its own folder and copy public into it.

Frequently asked questions

Why not just rename the folder?

Without usePublicPath(), Laravel still looks for public when it resolves public_path(), so the storage link and Vite assets would point at a folder that no longer exists.

Does this work for Laravel 10 and older?

This post covers Laravel 12. Older versions bootstrap the application differently in bootstrap/app.php, so the code above does not apply as written.

Is it safe to put index.php in public_html?

Yes, as long as only the public folder's contents are in the web root. The application code, vendor and .env stay one level above it.

Share:
Mahzaib Mirza

Written by

Mahzaib Mirza

Software developer & Founder of Coders Vibe.

Related Posts

Liked this post?

Get the next one in your inbox the moment it's published. No spam, unsubscribe anytime.

1 Comment

R

ryo

2/18/2023

Hi, thanks for the information, can u share an update for laravel 9? cause steps2 like webpack and server.php is gone now, wonder how to do it in larevel 9, thanks!

M

Mahzaib Mirza

2/18/2023

Hi, You can simply skip that server.php and webpack modifications. Let me know if still it's not working.

Leave a comment