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:
- Redirect visitors from http://boodebr.org to http://boodebr.org/main
- Tell Drupal that it is living under "/main" and not under "/"
- 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
This is a great tutorial and
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 found a way!
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!
Excellent!
Great tutorial. This was
-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.
Post new comment