December 12, 2024
Drupal Sub domain configuration

Big thanks to Ariene Ellefsen for writing this great little tutorial for DrupalSN, thanks Ariene!

Hello Everybody,

I’d like to share this tutorial with you about how to set up subdomains on your website using Drupal and a few server tricks.

So why use subdomains?
Subdomains offer 3 main advantages:

Google treats a sub domain as a totally different website from its main domain
You can use any keyword(s) as a subdomain
It doesn’t cost you any extra money to set up a subdomain (with most host companies).
So, if your agent website is hosted on a domain name like, “ellewebdesign.com“, you can set up an unlimited number of subdomains (providing you host company permits it, check with them), such as…

service.ellewebdesign.com
portfolio.ellewebdesign.com
blog.ellewebdesign.com
project.ellewebdesign.com
And so on. The good news is that the keywords in the subdomain name carry SEO importance, just like the keywords in the domain name. Combine that with the fact that Google sees each subdomain as a separate website, you’ve got a great way to create lots of backlinks.

Drupal Sub domain configuration

Here’s another Google advantage. As it is now, each search results page on Google will show a maximum of two listings from the same website. But if you set up subdomains, it’s possible to dominate all 10 listings on the first results page of Google!

So subdomains can offer big SEO (Search Engine Optimization) advantages as well as looking professional.

We have all heard by now the advantages of having a blog for promoting your website. So why not try it and create a blog using this technique and subdomains for your site!

Once you have your blog, adding a link to it on your menu takes a couple of seconds, and voila – your blog is up and running. This is the part where you feverishly start posting articles about your niche market, your product, or your service, and link to your website from each article.

Blogs are amazing: Search Engines love them, and they generally get good PR very quickly.

Good luck and I hope this tutorial will show you the importance of using subdomains.

Subdomain Setup
Let’s get stuck in and set up our subdomains.

If you want to test this you can download XAMPP or WAMP and have your local computer run like a server. Both packages are free and easy to set up.
For these examples we are using XAMPP for Windows.
It’s highly recommended that you test this locally before working on a live server and we recommend checking with your host before setting up subdomains as some hosts will offer a control panel to set up subdomains quickly and easily.
Step 1: Server Setup
1 First let’s create a DNS entries file for the website (The purpose of creating the DNS entry is to be able to test the sub domains in our local machine)

DSN (Domain Name System) tells your server where a subdomain or web address should be directed to. It’s basically a way of putting a human readable name over the top of an IP address.

2 To find the host file (that we can add our DSN entries to) go to: C:\WINDOWS\system32\drivers\etc\ then open the “hosts” file using Notepad.

Add the following entries, then save and close the “hosts” file:

DSN setup
3 Next we need to alter our Apache VirtualHost settings to look for the new subdomains.

Locate your servers httpd.conf or httpd-vhosts.conf file (which one you have depends on your server setup), if you are using XAMPP you’ll find it at:

c:\program files\xampp\apache\conf\extra\httpd-vhosts.conf
On Linux you will find your httpd.conf file at:

/etc/httpd/conf/httpd.conf
So go ahead and add the following to the bottom of your httpd.conf or httpd-vhosts.conf file:

Listen 80
DocumentRoot “C:/Program Files/xampp/htdocs” ServerName localhost:80
DocumentRoot “C:/Program Files/xampp/htdocs” ServerName www.site.local:80
DocumentRoot “C:/Program Files/xampp/htdocs” ServerName www.blog.site.local:80
You’ll need to restart Apache to this change to take affect.
On Linux use: apachectl -k restart
On XAMPP you can use the XAMPP control panel or restart your computer.

Great!! We already have two subdomains but to make it works we need to create a folder inside of our apache installation.

Step 2: Drupal “sites” folder setup
1 Go to: C:\Program Files\xampp\htdocs and create a folder called “www.site.local”, this is where your default Drupal installation will reside so add all your Drupal installation files to this folder.

Drupal Files
Proceed with the Drupal installation and test the site on your local machine.

Install Drupal

Step 3: Let’s create our subdomain
We going to create a “blog” subdomain (blog.site.local) for our main Drupal install (www.site.local).

1 Let’s add the new subdomain folder inside of C:/Program Files/xampp/htdocs/www.site.local/sites and call it “blog.site.local” (see screengrab below).

Sites Folder structure
The new “blog.site.local” folder will contain all the settings, themes and modules that are specific to the new subdomain, any modules or themes common to all sites should go in you sites/all folder.
Find out more about installing modules and themes.

2 Now we need to create a new settings.php file for Drupal to recognize that the blog.site.local is a subdomain of the main www.site.local domain.

To do this follow the following steps:
A) Copy the settings.php file from www.site.local/sites/default to the folder blog.site.local
Subdomainn settings file
B) Now we need to decide if we want our subdomains to share the same database or to have a different database for each subdomains (a Drupal multisite setup).
I will explain how to set up both. Let’s start with a single database for each subdomain:

Single Database Set up
1 Open the settings.php in our new “blog.site.local” folder and modify the $db_url and $db_prefix to reflect our database settings.
Let’s replaced the line below with our database, username and password:

2 Now we need to alter the $db_prefix variable so that all our database tables for this subdomain are preffixed with some text so we can distinguish between them and the main domain tables.

You can choose any prefix you want for your $db_prefix configuration, I’ll use ‘blog1_’:

And that is it!! Here you have a drupal site with subdomains using a single database!!!

Now just point your site to: www.blog.site.local and proceed with your installation. Each subdomain will have our own database.

Set up Multiple databases
Follow the exactly the same proceedure as with the sinle database set up but make sure you leave the $db_prefix blank:

And that is it!! Here you have a drupal site with subdomains using a multiple database!!!

Now just point your site to: www.blog.site.local and proceed with your installation. Your subdomain will share the same database.

I hope this tutorial is helpful!!! Thank you for reading!!