Skip to content

Laravel public folder: document root and .htaccess on shared hosting

· The AYT team · 5 min read

Developer desk with two monitors, one showing code

The Laravel public folder (public/) must be your domain's document root: it's the only folder meant to be reachable from a browser, and it holds index.php, which every request goes through. If the domain serves the project folder, files like .env (with your database passwords and APP_KEY) can be downloaded by anyone. The right fix is to change the document root to public/; if your host won't let you, there are three other options, each with different risks.

What the domain serves when the document root is the project folder, compared with public/

The Laravel documentation is blunt about it: the web server must send every request to public/index.php, and moving index.php into the project root exposes many sensitive configuration files to the internet.

Laravel public folder: how to check whether you have the problem

Open these on your domain in a browser:

  • https://your-domain.com/.env: if it downloads or shows text, every password in it is compromised; change them after fixing the document root;
  • https://your-domain.com/composer.json: if it shows, so does the rest of the project;
  • the site address without /public: if the site only works with /public in the URL, the document root is wrong.

Other symptoms: a file listing ("Index of /"), a 403 Forbidden error on the home page (the project folder has no index.php), or generated links with /public/ in them.

Option 1: change the document root (the right one)

Put the project in its own folder, for example /home/account/laravel, and set the domain's document root to /home/account/laravel/public.

In cPanel, under Domains, you can change the document root for addon domains and subdomains. For the main domain, the option depends on the panel version and the provider's settings. If you can't find it, ask support: it's a routine request.

On an nginx server, the same thing means the line root /home/account/laravel/public; in the site configuration, as in the Laravel documentation's example.

Option 2: a symbolic link to public

If the document root is fixed (for example public_html), you can replace it with a symbolic link to the project's public folder:

mv ~/public_html ~/public_html.old
ln -s ~/laravel/public ~/public_html

Without SSH, run these commands once through a job on the Cron Jobs page, then delete the job. Some hosts don't follow symbolic links for the document root, or recreate the folder on the next change in the panel, so check afterwards that the site works.

Option 3: move the contents of public into public_html

If you can neither change the document root nor use a link, copy the contents of public/ into public_html and keep the rest of the project in /home/account/laravel. Then edit public_html/index.php so it finds the project. The example is for Laravel 11 and newer; change only the lines below, the rest of the file stays the same:

if (file_exists($maintenance = __DIR__.'/../laravel/storage/framework/maintenance.php')) {
    require $maintenance;
}

require __DIR__.'/../laravel/vendor/autoload.php';

$app = require_once __DIR__.'/../laravel/bootstrap/app.php';
$app->usePublicPath(__DIR__);

$app->handleRequest(Request::capture());

The $app->usePublicPath(__DIR__) line matters: it tells Laravel the public folder is now public_html. Without it, public_path() still points at laravel/public: Vite reads its manifest from there, and storage:link creates the link in laravel/public/storage, where the browser can't reach it.

The downside: on every update you have to copy the new files from public/ into public_html again (especially public/build after npm run build).

Option 4: an .htaccess that sends everything to public (last resort)

On Apache, an .htaccess in the project root can rewrite every request to public/:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteRule ^(.*)$ public/$1 [L]
</IfModule>

<FilesMatch "^\.">
    Require all denied
</FilesMatch>

Requests land in public/, where the .htaccess that ships with Laravel passes them on to index.php. The second block blocks files starting with a dot, .env included.

The risk: everything depends on mod_rewrite and on the server reading .htaccess. If the provider changes the configuration, moves the site to another server or switches to nginx, which ignores .htaccess entirely, the whole project is exposed again. Use this only temporarily, and test /.env again after any change at your host. The differences between Apache and nginx are explained in the article on .htaccess on nginx.

After the fix

  • If .env was ever reachable, change the database password and any API keys in it, and generate a new APP_KEY. Note that a new APP_KEY invalidates sessions and data encrypted with the old one.
  • Check APP_URL in .env: it must be the address without /public.
  • Clear the configuration cache (php artisan config:clear or php artisan optimize) so no old paths linger.
  • If uploaded images don't show, the public/storage link needs recreating; see storage:link without SSH.

Frequently asked questions

Can I leave the site working with /public in the address?

The site works, but the problem remains: if /public appears in the URL, the project root is being served, so .env, storage/ and vendor/ are one address away. The fix is still a document root set to public/.

Does the .htaccess option work on nginx?

No. nginx doesn't read .htaccess files, so the rules have no effect and the project stays exposed. On nginx, the document root is set in the server configuration.

On AYT hosting

On AYT you don't have to choose the document root for Laravel. When you pick Laravel in Create a New Domain or press "Make this a Laravel site" on a domain, the panel creates the releases/, shared/ and current layout, and the site is served from current/public. Files already in the domain's folder stay there but are no longer served, and the conversion can't be undone.

The Create a New Domain form with Laravel in the app list

The web server is nginx, so .htaccess isn't read and options 3 and 4 aren't needed. The nginx profile for Laravel blocks sensitive files. You bring the code in with Git deploys, with a deploy script that runs composer install, the build and the migrations, as in the guide to Laravel on hosting without SSH. See the supported apps and the plans.

More articles

All articles