How to move Drupal to a subdirectory and keep the links

2 min read >

How to move Drupal to a subdirectory and keep the links

Engineering Insights & Web Platforms

The usual way for Drupal is to have it live at the root of your website. It’s all nice if the only thing on your site is Drupal, but it gets very cluttered when you have other top-level directories.

One customer had his corporate site at, say, ““, and his customer support pages at ““.  We wanted to keep the application in the “support” directory, and move all the drupal directories under “corporate“. And keep the old links working.

First, we had to let Drupal know that, even if it’s been relocated to “corporate”, the URL still remains the same. Do it in “corporate/sites/default/settings.php“:

$base_url = '';

Then tell Apache that Drupal has moved. Put these rewrite rules in .htaccess at the root of your website.

  RewriteEngine on
  # Rewrite to the Drupal home page:
  RewriteRule ^$ corporate/index.php [L]
  # Let Drupal process paths like
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteRule ^corporate/(.*)$ corporate/index.php?q=$1 [L,QSA]

  # Search for real Drupal files that moved, 
  #  (e.g. to
  RewriteCond %{DOCUMENT_ROOT}/corporate/$1 -f
  RewriteRule ^(.*)$ corporate/$1 [L]

  # Let Drupal process all paths that are not real files and directories, like
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_URI} !=/favicon.ico
  RewriteRule ^(.*)$ corporate/index.php?q=$1 [L,QSA]

  # What's left are real non-Drupal pages and directories, like