How to Install Drupal 8 in a Subdirectory

Website Administrators usually install Content Management Systems in a subdirectory when they use them for their websites. This is a good practice because in case you want to use another CMS in some future time and stop using your present CMS, the files from both CMS’s will not get mixed up.

Drupal is one of the top CMS’s that power most of the websites on the internet. Web Administrators love it because you can do a lot of things with it even without coding, provided that you spend the time to learn it. If you want to install Drupal in a subdirectory instead of the root directory of your web host, this tutorial is for you.

If you follow the steps below, remember to use the actual domain name of your website and the name of your Drupal installation folder instead of the words example.com and drupal, respectively.

1. Install Drupal in a subdirectory

Create a folder in your web host where you want to install Drupal. After which, decide how you want to install Drupal: manually or using a piece of software. If you want to install Drupal manually, read this step-by-step tutorial. If you want to install it using a piece of software, read this article instead.

2. Create a .htaccess file in the root directory

Open a text editor and create a .htaccess file in the root directory of your web host. Write the following lines of code in it:

# Installed Site In Subdirectory
RewriteRule ^$ drupal/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ drupal/$1 [L]
3. Add a piece of code to the subdirectory .htaccess file

Look for the .htaccess file in the subdirectory where you installed Drupal. Write in it the code below:

# Installed Site In Subdirectory
RewriteBase /drupal
4. Modify the settings.php file

Using an FTP client or the cPanel, go to the Sites directory and change the permissions for the folder called default to 0755. By default, the folder default has the folder permissions of 0555. After changing the folder permissions, look for the file settings.php inside the folder. Open settings.php and paste the following lines in it immediately after the PHP opening tag at the top of the page.

// Force HTTPS
// PHP_SAPI command line (cli) check prevents drush commands from giving a
// "Drush command terminated abnormally due to an unrecoverable error"
if ( (!array_key_exists('HTTPS', $_SERVER)) && (PHP_SAPI !== 'cli') ) {
  header('HTTP/1.1 301 Moved Permanently');
  header('Location: https://example.com'. $_SERVER['REQUEST_URI']);
  exit();
}

// Remove www to make the URL short
if ($_SERVER['HTTP_HOST'] == 'www.example.com') {
  header('HTTP/1.0 301 Moved Permanently');
  header('Location: https://example.com'. $_SERVER['REQUEST_URI']);
  exit();
}

// Remove installation folder slug from URL
if (isset($GLOBALS['request']) and '/drupal/index.php' === $GLOBALS['request']->server->get('SCRIPT_NAME')) {
  $GLOBALS['request']->server->set('SCRIPT_NAME', '/index.php');
}

// Trusted host configuration to prevent HTTP Host header spoofing
 $settings['trusted_host_patterns'] = [
   '^example\.com$',
 ];

If you are running multisite, or if you are running your site from different domain names (e.g., you do not redirect http://www.example.com to http://example.com), you should specify all of the host patterns that are allowed by your site. Use the code below instead of the last lines of code above. Check the line that starts with a double slash.

// Trusted host configuration to prevent HTTP Host header spoofing
$settings['trusted_host_patterns'] = [
   '^example\.com$',
   '^.+\.example\.com$',
   '^example\.org$',
   '^.+\.example\.org$',
 ];

After modifying the folder default, change its folder permissions back to 0555.

5. Logging in to the site

You can log in as Administrator to your Drupal-powered website at example.com/user/login. You will need to clear the Drupal cache first if you want to work on your site right away.