Installing Drupal in a subdirectory

Revealed by sykora within web on July 18th, 2008

If you’re like me, when you install something like drupal, you probably do a lot of tinkering around. In order to limit the chaos I would make, I installed drupal in a separate directory under my public_html/, so that I could remove it quickly if anything went wrong.

However, there comes the problem of rewriting urls. This is because as you may imagine, urls such as http://example.com/drupal/… are very irritating. It’s much nicer to have the drupal/ taken out, so that it appears as if you’ve installed at the root, but maintaining the installation in a subfolder.

Enter apache’s mod_rewrite. The following snippet I found (here, after great difficulty) will allow you to run drupal off a subdirectory while maintaining primary urls. This code is, of course, with respect to my own site.

RewriteEngine on
RewriteCond %{HTTP_HOST} !^lucentbeing\.com$ [NC]
RewriteRule .* http://lucentbeing.com/ [L,R=301]
RewriteRule ^$ drupal/index.php [L]
RewriteCond %{DOCUMENT_ROOT}/drupal%{REQUEST_URI} -f
RewriteRule .* drupal/$0 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* drupal/index.php?q=$0 [QSA]

To adapt this, replace lucentbeing with the base url of your site, and drupal with whatever folder your drupal installation lives under. The code should then go in the .htaccess file in your public_html folder (DocumentRoot).

The only remaining thing to do, is to open up settings.php in sites/default and change $base_url to the url you want to be the base, such as http://lucentbeing.com. Note that this may be tricky due to permissions issues. I just worked around it by downloading the file, deleting from the server and uploading it again.

Using this workaround allows you to keep drupal in a separate folder, making easier for you to update/remove/poke around, while still maintaining a production site. It’s especially useful when setting up the first time, so you can easily delete the drupal/ folder and try again without too much confusion.

A few caveats :

  • This won’t remove urls of the form site.com/drupal/… They’re still there.
  • The www->non-www rewrites as specified in the .htaccess in drupal/ won’t work, at least they didn’t for me. To fix this, copy those 2 lines into your main .htaccess, at the top.

Are there any easier/simpler/more preferred ways of doing this?

Please enlighten me.

Tags: , ,

Leave a Reply

Powered by Wordpress, valid XHTML and CSS. Original theme by Themelab, modifications by me.
Gatekeeper