How to install drupal in a sub-directory

I tried to install a drupal site in a sub-directory of a domain and I ran into some problems. I will explain here how I solved them.

Many forum topics and issues on drupal.org and elsewhere on the web discuss how to install drupal in a sub-directory of your domain and to have it appear as if it were at the root of your domain, e.g.:
– drupal is in www.mydomain.com/sub-dir, but your webserver opens your drupal site when you go to www.mydomain.com.

I — on the other hand — wanted to have it installed in a sub-directory and have it APPEAR in that subdirectory, i.e.:
– drupal is in www.mydomain.com/sub-dir and the webserver opens the drupal site when people go to www.mydomain.com/sub-dir. Thus the top domain could have some very light page with just a few links which redirect the people to different sections of your site which may each be a seprate drupal site or they be implemented with totally different system, eg. one drupal and another one – WP, etc..

The problem when drupal is installed in a sub-directory is that this breaks drupal’s “Clean URL’s” system.

Here’s what you need to do to have druapl work from a sub-directory:
1. Add the following to your .htaccess file:
RewriteBase /sub-dir
Note that this goes into the .htaccess file in the “sub-dir” subdirectory, not your website root-dir htaccess file!

2. You DO NOT need to modify the $base_url setting in your sites/default/settings.php file. Many tutorials and forum topics say that you need to modify your base_url to point to www.mydomain.com/sub-dir but that was not necessary in my case. Just to be as exhaustive as possible – I did try it with the base_url modified and it also worked for me but I then commented it off again since everything worked fine without it.

– – –

If you also want your visitors to be able to access you drupal site via a sub-domain name, that is open the drupal site in the sub-directory when they go to e.g.: shop.mydomain.com, you can achieve this via subdomain-redirection:

Go to the sub-domains section of your cPanel (or whatever your host provides as a tool to manage sub-domains) and redirect the shop.mydomain.com subdomain to “www.mydomain.com/sub-dir”. This way whenever people try to open shop.mydomain.com they will be redirected to your drupal site. Now, mind you the URL in the browser will also be changed to “www.mydomain.com/sub-dir”, so they will not see e.g. shop.mydomain.com/products but “www.mydomain.com/sub-dir/products”. IMHO this is not a big deal – just name your subdirectory the same as the sub-domain you want to use, i.e. in the above example name both the subdoamin and sub-directory “shop”.

3 thoughts on “How to install drupal in a sub-directory

    • … ah yes – good point, thank you 🙂

      The .htaccess file where you add the
      RewriteBase /sub-dir
      command is the one in the subdirectory. In my particular case I dont’t even have a htaccess file in the main site dir.

  1. And a similar problem I just finished banging my head against:

    I wanted to move a live drupal site to my localhost for testing. To a sub-directory on my localhost to be exact.

    At my hosting provider the site appears at the top-level directory of the domain, i.e. at http://www.example.com.
    On my local host the top-level is already used for something else and I did not want to break it so instead I wanted my site to be accessible locally as http://localhost/example

    I created the ‘example’ sub-directory in my doc-root, copied my site there, added the database and… it did not work 🙂

    Well.. it KINDA worked… it shoed the main page but none of the links worked. After fiddling with drupal’s base_url (in files/default/settings.php) and with RewriteRule and RedirectMatch commands in my .htaccess file I finally solved the problem really easy:
    – Simply disable Clean URLs 🙂

    I realize that this may not be the best solution for everybody, especially if you need to test some of your clean URL paths but I could not get it working the other way, so there… this works for me.. 🙂

    I would of course appreciate any tips on how this could be done without disabling Clean URLs. But I think this is not an easy nor universal task since people (including myself) often have link mappings withing drupal. And sometimes also hardcoded URLs (I dont use these.. learned my lesson long time ago 🙂 ).

Leave a Reply

Your email address will not be published. Required fields are marked *