Hosting a drupal instance from a sub-directory (using Apache mod_rewrite)
If you are like me, you are probably adverse to committing any portal or CMS solution to your root directory. You never know when you may want to completely swap to another solution and have that hosted at your root domain.
I have found numerous posts, but very few successful answers on how to host Drupal in a sub-directory. If you want to host Drupal in a sub-directory (for example <document root>/drupal), but want to access your Drupal instance at http://www.yourdomain.com (instead of http://www.yourdomain.com/drupal), here is how you do it. Note: this example assumes that you with to host in a sub-directory named "drupal". Simply replace "drupal" with the actual name of your sub-directory if it is different.
- In your document root, create a .htaccess file with the following directives:
-
<IfModule mod_rewrite.c>
RewriteEngine On
# Send requests with query strings that do not match existing directories or files to Drupal
RewriteCond %{REQUEST_FILENAME} !-fRewriteCond %{REQUEST_FILENAME} !-dRewriteRule ^(.*)$ drupal/$1 [L]
# Send all bare requests directly to drupal
RewriteRule ^$ drupal/ [L]
</IfModule>
- Your original .htaccess in the Drupal sub-directory should remain in-tact. Ensure that RewriteBase is not set (preceded with a #). Here are the relevant portions of drupal/.htaccess
-
<IfModule mod_rewrite.c>
RewriteEngine on
# Rewrite URLs of the form 'x' to the form 'index.php?q=x'.
RewriteCond %{REQUEST_FILENAME} !-fRewriteCond %{REQUEST_FILENAME} !-dRewriteCond %{REQUEST_URI} !=/favicon.icoRewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
</IfModule>
- Update sties/default/settings.php and set $base_url to your root domain. Ensure that you do not follow the URL with a slash
-
$base_url = 'http://www.yourdomain.com';
- If you have already struggled to create this configuration on your own, make sure that your clear your Drupal cache now. Otherwise, Drupal may continue to serve your bad links to non-existant pages or pages that contain the sub-directory.
Comments
Thank you so much for this, Ive read so many tonight that are overly complicated!
Simple and works!
Post new comment