Having Drupal live in a subdirectory

The easiest way to install Drupal is to have it live in the root of your webserver. However, I don't like doing that, because I have other top-level directories under my webroot, and I don't want to clutter up the root with the Drupal files. Having Drupal in a subdirectory makes maintenance a lot easier (IMO, of course).

For reference, my webserver is set up like this:

  • boodebr.org root = /var/htdocs/boodebr
  • Drupal root = /var/htdocs/boodebr/main
  • boodebr.org is an Apache VirtualHost (the real root is /var/htdocs).

I have to do three things to set it up the way I want:

  1. Redirect visitors from http://boodebr.org to http://boodebr.org/main
  2. Tell Drupal that it is living under "/main" and not under "/"
  3. Tweak the Apache httpd.conf so that URL rewriting will work under the VirtualHost.

Since Drupal will live in "/main", the first thing I want to do is redirect visitors from "http://boodebr.org", to "http://boodebr.org/main". While it's possible to do this with a short "index.html" file, it is faster to do it with mod_rewrite. This also makes the redirection invisible to the end user which gives a nicer frontpage URL. To do this, I create a file ".htaccess" located in /var/htdocs/boodebr:

# this redirects "/" to "/main" (while maintaining the original URL),
# (Drupal still adds the '/main' back in when you click on a topic,
# but having a clean frontpage URL is my main concern here.)

RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST} boodebr.org
# don't rewrite if it's already /main
RewriteCond %{REQUEST_URI} !main/
# don't rewrite requests for /javascript
RewriteCond %{REQUEST_URI} !javascript

# if it satisfies those conditions, here's the rewrite rule
RewriteRule ^(.*)$ http://boodebr.org/main/$1

 

Note that I had to add a rule to ignore a toplevel directory (/javascript) that shouldn't be rewritten. There may be a more generic way to do this, but I'm new to mod_rewrite.

The second thing that I need to do is modify the .htacess file that lives in the Drupal root (/var/htdocs/boodebr/main/.htaccess, in my case). Uncomment and edit the 'RewriteBase' line, for example:

  # Modify the RewriteBase if you are using Drupal in a subdirectory and
# the rewrite rules are not working properly.
RewriteBase /main

In the Apache httpd.conf file, my VirtualHost looks like:


<VirtualHost *>
ServerName boodebr.org
ServerAlias www.boodebr.org
DocumentRoot /www/htdocs/boodebr
</VirtualHost>

This is fine, but I need to add a <Directory> container above it (not sure if ordering matters) to make URL rewriting work for my VirtualHost. I added the following directive immediately before the <VirtualHost> container:


<Directory "/www/htdocs/boodebr">
AllowOverride All
<Directory>

After those modifications, URL rewriting works perfectly under Drupal. To turn it on, simply go into Settings->General Settings, run the "Clean URL test" (which should now work OK), and Enable clean URLs.

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

This is a great tutorial and

This is a great tutorial and it totally worked for me. Now I've got a multi-site set up where I have sub directories (named example1.com, example2.com, etc) under the main "sites" directory. But I cannot seem to access those sites, and I was able to before. Of course this was back when my drupal files lived directly under the publi_html dir and not in a sub dir under the root. But now when I move drupal to a sub dir, I can no longer access my other sites.

I have even made the database line adjustments to code in the settings.php files for each site dir. How does one go about having a multi-site installation of drupal and still have everything under a sub dir. I would greatly appreciate your help, thanks.

Best, Bao

Multisite

I'm not sure, to be honest. I do it the hard way and have a separate Drupal installation for each site. I would love to figure out how to do it with a single installation but haven't spent the time to do it yet.

I found a way!

Hi Frank!

Its great to hear from you! I was able to combine your tutorial with some more research that I did online line and now am able to pull off a multi-site capability for a single drupal installation that lives in a single sub directory underneath the public_html. I was able to apply your tutorial to hide the "main" sub directory for the front page of each site in my multi-site installation.

I would love to be able to post my methods on your site if you let me and when I get some down time in the near future. I wouldn't have been able to pull this off without stumbling accross your tutorial and now I would like to give back to the rest of the drupalers out there.

Best, -Bao

PLEASE post your methods!

I have some show-stopper issues that would be solved if I could figure out how to do this! If you've already figured it out, please post your solutions, I would GREATLY appreciate it, and I am sure others would as well! Thanks in advance!

Excellent!

I can't wait to see your additions so I can fix my own mess of sites! If you want to format it nicely, here is the markup helptext used here. You can either post it as a comment (let me know if I should move it into the main text), or you can send it to me at fmcingvale +at+ boodebr -dot- org


Great tutorial. This was

Great tutorial. This was exactly what I was looking for, now I have a question, how would you apply this set up to a multi-site set-up? I have the following directories in my sites folder, example.com, example2.com, etc. How would I set the settings.php in each directory, or how do I adjust the .htaccess to be able to hide the "main" dir and have a multi-site setup? I was successful in setting up a multi-site but after applying the methods in this tutorial, I have lost multi-site access. Any help and input is much appreciated. Thank you.

-Bao

Maybe a fourth step

There may be a fourth step:

4. Tell Drupal to generate URLs without 'main/' in it, so you get http://www.example.org/node/XXX instead of http://www.example.org/main/node/XXX.

See http://drupal.org/node/135206. One would set:
$base_url = 'http://example.org';

in settings.php.

Of course one does not have to do that if you actually want URLs in the form of http://example.org/main/node/XXX because for example Drupal is only handling a subdirectory of the site.

I was following this post and didn't understand why it wasn't working like I thought you explained it would work (misread it). So this may help others.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Post new comment

The content of this field is kept private and will not be shown publicly.