Moving Craft’s Files Below the Webroot

We recommend that all of Craft’s PHP files live above your server’s webroot, but sometimes that may not be possible due to hosting restrictions.

If you need to restructure your files so that everything lives below your webroot, follow these steps.

Step 1: Create a new app/ folder #

Create a new folder within your webroot called app/ (e.g. web/app/ if you’re sticking with Craft’s default web/ folder for your webroot).

Step 2: Protect the app/ folder #

If your web server is running Apache, and you’re allowed to create .htaccess files (via the AllowOverride directive), then create a new .htaccess file within your app/ folder, with this content:

deny from all

If you’re running Nginx or aren’t able to use .htaccess files, then you’ll need to ask your web hosting provider how to protect your app/ folder from public HTTP traffic. If this isn’t possible, then don’t proceed.

Step 3: Move the application files #

Move the following project files into your new app/ folder:

  • config/
  • modules/
  • storage/
  • templates/
  • translations/ (if you have it)
  • vendor/
  • .env
  • .env.example
  • bootstrap.php (if you have it)
  • composer.json
  • composer.lock
  • craft
  • craft.bat

At this point, your project’s file structure should look like this:

└── web/
    ├── app/
    │   ├── config/
    │   ├── modules/
    │   ├── storage/
    │   ├── templates/
    │   ├── vendor/
    │   ├── .env
    │   ├── .env.example
    │   ├── .htaccess
    │   ├── composer.json
    │   ├── composer.lock
    │   ├── craft
    │   └── craft.bat
    ├── index.php
    └── ...

Step 4: Change the CRAFT_BASE_PATH #

Finally, we need to tell your index.php file where Craft has been moved to. Open it up and find this line:

define('CRAFT_BASE_PATH', dirname(__DIR__));

Change it to this:

define('CRAFT_BASE_PATH', __DIR__ . '/app');

Once you’ve done that, Craft should be back up and running, loaded from your new app/ folder.

Don’t forget to change to your app directory before running Craft console commands! For example, you’ll need to cd app from the project root before running php craft up.

Applies to Craft CMS 3.