Linux::debian::drupal

From www.deloptes.org
Jump to navigation Jump to search

Having Drupal live in a subdirectory

June 18, 2006 - 12:35pm — frank

http://boodebr.org/main/server-setup-checklist/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:

   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:

  1. this redirects "/" to "/main" (while maintaining the original URL),
  2. (Drupal still adds the '/main' back in when you click on a topic,
  3. 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.