User login

Login/Register
To prevent automated spam submissions leave this field empty.

Drupal Tutorials

Drupal Sub domain configuration

21
Saturday, February 21, 2009

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:

  1. Google treats a sub domain as a totally different website from its main domain
  2. You can use any keyword(s) as a subdomain
  3. 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.

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
<VirtualHost *:80>
    DocumentRoot "C:/Program Files/xampp/htdocs"
    ServerName localhost:80
</VirtualHost>
<VirtualHost www.site.local:80>
    DocumentRoot "C:/Program Files/xampp/htdocs"
    ServerName www.site.local:80
</VirtualHost>
<VirtualHost www.blog.site.local:80>
    DocumentRoot "C:/Program Files/xampp/htdocs"
    ServerName www.blog.site.local:80
</VirtualHost>

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:
<?php
//$db_url = 'mysql://username:password@localhost/databasename';
$db_url = 'mysql://drupal:drupal@localhost/drupal_db';
?>

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_':

<?php
$db_prefix
= '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:
<?php
$db_prefix
= '';
?>

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!!

Ariene Ellefsen
http://www.ellewebdesign.com

Comments (12)


Login or register to post comments
Imafilmmaker's picture

Some really great SEO tips

Some really great SEO tips about subdomains! Many people overlook the importance of using keywords in the URL. Thanks so much for this tutorial. Do you know how to do this using MAMP on OSX? I'm not sure I understand how to access the DNS on a mac.

I use this to manage my

I use this to manage my local hosts on my Mac - very handy for off-line development: http://www.northernsoftworks.com/hostal.html

nazar2k2's picture

Really a fantastic tutorial

Really a fantastic tutorial for creating the sub domains.thanks for this great tutorial.Also can you please tell me how to do this using Lamp on OS?

Thanks

http://www.drupal-web-developers.com

Thank you for this great

Thank you for this great info, I was struggling to write a subdomains for very long, eventually i got idea here.
fat burning furnace

This is a great tutorial. I

This is a great tutorial. I was having some issues with this when setting up a subdomain. It turns out the issue was with my GoDaddy account. I finally got my subdomain resolved, but this tutorial is still helpful nonetheless.

This tutorial has give me

This tutorial has give me clear instruction on set up subdomains on my website using Drupal and a few server tricks. -- fat burning furnace and fatburningfurnace review

The new "blog.site.local"

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.
jump higher and Mp3 rocket pro

Thank you for another great

Thank you for another great article. Where else could anyone get that kind of information in such a perfect way of writing? I have a presentation next week, and I am on the look for such information.

free advertising |job|iphone repair

Great!! We already have two

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

I havent tried drupal before

I havent tried drupal before but now i feel like trying drupal... lets see how it would feel.. thanks for the tut of Pure Acai Berry

WiredTree.com Coupons. I

WiredTree.com Coupons. I needed a new server, and I already had a Wiredtree VPS which I was 100% sat­is­fied with. I decided I’d try out a huge pack­age from Wiredtree! I needed some­thing big for the web­site I was run­ning that was get­ting thou­sands of hits a day. I used this WiredTree.com Coupons and it saved me over $300 dol­lars a month on the total offer. Wicked!

royalt's picture

That's a very useful plugin,

That's a very useful plugin, I'm gonna use it on my Drupal site.
online games
dress up games for girls