Drupal multisite in subfolders

Printer-friendly version

We all know you can run several Drupal sites using a single codebase. This is done using Drupal's multisite feature. Most of the time you want to host all sites on a different domain name. There's a lot of documentation on how this can be accomplished.

But sometimes you want to run the sites in a subdirectory on one domain. You may want to do this to run a development server for a multisite. You can host all sites in a subdirectory then. This can be easily done with some extra Apache configuration.

Suppose your domain name is mydrupalsite.com. You want to host a bunch of site using Drupal's multisite feature in the subdirectories

http://www.mydrupalsite.com/subdir1
http://www.mydrupalsite.com/subdir2
http://www.mydrupalsite.com/subdir3

You have to do 3 things.

Add alias your Apache configuration file

We want requests for the 3 subdirectories to go to the same Drupal instance. We can do this using Apache's Alias functionality.

Alias /subdir1 /var/www
Alias /subdir2 /var/www
Alias /subdir3 /var/www

I'm supposing here Drupal's codebase is hosted in /var/www on your machine.

Redirecting all requests to index.php

Now that we're serving all requests from one codebase we have to redirect all requests to index.php. This needs to be done since all Drupal requests are served from one endpoint, called index.php.

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/subdir1/(.*)$
RewriteRule ^(.*)$ /subdir1/index.php?q=$1 [L,QSA]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/subdir2/(.*)$
RewriteRule ^(.*)$ /subdir2/index.php?q=$1 [L,QSA]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/subdir3/(.*)$
RewriteRule ^(.*)$ /subdir3/index.php?q=$1 [L,QSA]

Creating our settings.php

You can now have different settings (database, ...) for each site by creating a different settings file for each site. Place the settings.php for each file under a directory called after the domain name and subdirectory. In our example we would have the following setting files:

/var/www/sites/mydrupalsite.com.subdir1/settings.php
/var/www/sites/mydrupalsite.com.subdir2/settings.php
/var/www/sites/mydrupalsite.com.subdir3/settings.php

Comments

drupal rules!

drupal rules!

how to share session in multiple website

I create multiple website follow your steps, but how to sharing the session on this multiple website, let user can access all multiple website.

thank you

the .htaccess piece was what I was missing to get clean urls working. made my day, thanks!

How can I do in Window WAMP

How can I do in Window WAMP server?
http://localhost/site1
http://localhost/site2

symbolic link

can anyone tell me how can i create symbolic link on my server not my host using php script.

Custom Website Development

Really intresting and helpful post.Thanks for sharing information.

Reply to Azuledu

Are you sure that the clean url will work for both www.mysite.com and www.mysite.com/subdir1?

Simpler

I had problems with alias. I get it working in a simpler way:

My drupal is under /var/www/drupal and I want 2 diferent sites at myserver.com/site1 and myserver.com/site2

I only need 2 simple steps:
1.-Create var/www/drupal/sites/myserver.com.site1, copy settings.php inside and setup database conection string at settings.php

2.-Create simbolic links at /var/www
sudo ln -s /var/www/drupal /var/www/site1
sudo ln -s /var/www/drupal /var/www/site2

And nothing else. It works perfectly.

Update to my own post and

Update to my own post and apologize to the original author Davy Van Den Bremt. You method works partially. I can now have both my main drupal and sub-folder drupal work following your method. However, the clean urls function does not work on the subfolder. I can select "enable" under Administer/Site configuration/Clean URLs in my sub-folder installation. But after I clicked "Save configuration", it brings me to the login page of my main drupal installation, and says "Access denied. You are not authorized to access this page.". The URL showed in browser was still /subdir1/admin/settings/clean-urls.

The trick is RewriteRule and RewriteBase. In fact, in my case, I found that your Rewrite script did not affect anything. I copied yours in .htaccess of my main drupal installation. The original rewrite script in .htaccess file works fine. I changed the RewriteBase from / to /subdir1, it worked. But, do the reversed thing as I described above. e.g. when set to use clean urls in main drupal. it brings me to the log in page of my sub-folder installation.

My test indicates that your

My test indicates that your way does not work.

It can find index.php file, but I always get a "page not fond" error when I visit

http://www.mydrupalsite.com/subdir1

The only way that I found works is create a symlink back to the home directory where drupal is installed, as Boris Mann suggested.

Drupal in subdirectory and multi site in subdirectory

I have drupal installed at sub directory http://www.m4manas.com/6 I am able to access the site.

Now, i want to have multi site facility in this setup using same code base. I have installed a test site in sites/test
and modified following
1. .htacess to tell that drupal base is /6\
2. Update .htacess with the code
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/test/(.*)$
RewriteRule ^(.*)$ /test/index.php?q=$1 [L,QSA]
3. $base in settings at sites/test to show that the base is /6
4. Create a symlink pointing to base path

I am getting page not found error please help.

simpler .htaccess

in htaccess:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteCond %{REQUEST_URI} ^/(\w+)
RewriteRule ^(.*)$ /%1/index.php?q=$1 [L,QSA]

This will work so long as each subdirectory contains only alphanumeric characters. (another regex might be more versatile, but it must stop at the first slash.)

With this you don't have to edit your .htaccess every time you add a site.

Davy Van Den Bremt's picture

mollom installed

I have, but sometimes it lets something slip through.

mollom to avoid spam

You may want to install the free mollom version to avoid comment spam like the one above.

Some updates

You don't need to alias or change rewrites, you can just create a symlink in /var/www. So, ln -s /var/www /var/www/subdir1.

As well, in sites, the dot is the correct separator for subfolders -- sites/mydrupalsite.com.subdir1

I updated the multisite documentation to make this clearer: http://drupal.org/getting-started/6/install/multi-site

That page could use further editing. Please add your edits!

Another way

There perhaps is another way to do this that is simpler: no need to change Apache settings or Drupal rewrite code. First, install Drupal under the web root directory (e.g., drupal_site). Then, create site-specific folders in the following format:

cp -R drupal_site/sites/default/  drupal_site/sites/mysite.com.drupal_1
cp -R drupal_site/sites/default/  drupal_site/sites/mysite.com.drupal_2

Give each site-specific folder its own settings.php and database. Finally, make symbolic links from the web root directory to the site-specific folders, where the symbolic links are given your desired subdirectory names:
ln -s drupal_1 drupal_site
ln -s drupal_2 drupal_site

Now you can browse to mysite.com/drupal_1 and mysite.com/drupal_2 and get different sites. There are probably limitations to this approach, but it worked for me. One limitation may be that some platforms don't support symbolic links (e.g., Windows -- I don't know if shortcuts would be interpreted similarly by the web server.)

Redirecting all requests to

Redirecting all requests to index.php

Are you doing this in the .htaccess in the Drupal root?

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
  • Images can be added to this post.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <bash>, <javascript>, <mysql>, <php>. Beside the tag style "<foo>" it is also possible to use "[foo]". PHP source code can also be enclosed in <?php ... ?> or <% ... %>.

More information about formatting options