Having Drupal live in a subdirectory
June 18, 2006 - 12:35pm — frank
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.)
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.