Accessing Drupal 8 Installed in Subdirectory

Accessing Drupal 8 Installed in Subdirectory

There are a few outdated methods floating around the Internet on loading Drupal 8 from a subdirectory. One day, perhaps this article will suffer the same fate.

Goal:

  • Leave access open to other directories in web root, otherwise you might as well install Drupal in root and skip the whole darn setup customisation. e.g. Domain-level forwards are sometimes suggested.
  • Don't hack the core. Some articles suggest doing just that, and the solutions received a lot of "worked for me" comments. Poor for site maintenance. Potential security holes. Bad.

Our Drupal install is in /drupal, and that's what I'm using in the below examples. Adjust accordingly.

1. In the web root of the account, create an .htaccess file which contains the following:


  RewriteRule ^$ drupal/$1 [L]
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteRule ^(.*)$ drupal/$1 [L]

2. In the .htaccess file that is in the root directory of the Drupal installation, un-comment the RewriteBase line and add your subdirectory to it.

RewriteBase /drupal

Many actions will now work when accessed at the root domain name, but in some places you'll likely get the Redirects to external URLs are not allowed by default, use \Drupal\Core\Routing\TrustedRedirectResponse for it WSOD.

3. Add the following to the settings file in /drupal/sites/default/settings.php:

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

Leave a comment if you encounter problems, or have suggestions for a better method.

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.