Sunday, 3 February 2008

Making Drupal exist in a subdirectory

I have a confession: I am a bit of a neat freak when it comes to my files. Clutter annoys me almost as much as compressed files that extract themselves into the current working directory.
I set out on a quest today, to upgrade Drupal AND to make it happily exist in a subdirectory on my server, BUT, to make it appear that it exists in the root directory.


Actually just that turned out to be easy, Drupal even provides a guide that conveniently assumes you don't have any other content in your directory. I mean, you've got Drupal, why would you want a /photos directory that's not "inside" Drupal? Or a /files directory that Drupal doesn't manage? Stop being silly.


It turns out, though, that what I wanted was also quite easy, but piecing together the bits and cutting the cruft took excessively long.  But that's a typcial computery solution: A simple solution and a long and winding road to find it.


So, let's begin.



  1. Put Drupal in a subdirectory. Mine is called drupal-5.7

  2. Add a .htaccess to the root of the website to rewrite incoming requests to Drupal:
    RewriteEngine on
    RewriteRule ^$ drupal-5.7/index.php [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ drupal-5.7/$1 [L]

    Now, I'm not a .htaccess expert, so I may be slightly wrong in how exactly this works. The first RewriteRule directs the server to redirect all requests for "/" to Drupal's index.php, so drupal is what you see when you just type the base URL. The .htaccess inside the drupal directory can further rewrite this.
    The two rewrite conditions are for the next rule, !-f and !-d mean that only do this next rewrite if the file(or directory) doesn't already exist. I have /photos on my server that I don't want rewritten to /drupal-5.7/photos/
    The final rule simply appends the prefix drupal-5.7/ to the request, and sends it off. Drupal further rewrites this when Tidy URLs are used.

  3. Delete /index.php, we don't want the server to find it.

  4. Make sure you don't already have any existing files/directories that are inside the Drupal install. For example, I already use a /files directory, so does Drupal, so I renamed Drupal's to 'dfiles', and changed the config accordingly (Administration -> File System).


Cake. It all works as far as I can tell.

0 comments:

Post a Comment