الاثنين، 4 أكتوبر 2010

Setting up Wildcard DNS for Subdomains on Cpanel

Ever wanted to setup subdomains on your Cpanel server, such that a keyword is the wildcard part of your domain? Something along the lines of:
  • keyword.yourdomain.com
  • keyword2.yourdomain.com
  • keyword3.yourdomain.com
It's not as difficult as you might think, however, you will need access via SSH to edit a few server files, or, contact your web host to make a few quick edits on your account.

Step 1

First things first. The server. You must have access to "Edit DNS Zone" on your server WHM or Cpanel. This option is listed under DNS Functions. We'll assume you already have a domain on your server you want to test this with. If not, you'll need to create your new account as you normally would.

Click "Edit DNS Zone"
You should have a list of your domains. Select the domain you wish to use/edit.
Be sure not to edit any of these, but as noted!
Scroll down till you see "Add New Entries Below this Line"
You'll need to put an * in the first box, followed by adding your server IP address in place of "65.66.67.68" . Your IP address should be in one of the other entries in this screen where you can easily copy/paste it.
Here is a screen shot of what it looks like:
picture of cpanel when setting up wildcard dns
So it's only two entries here.

Step 2

The next step involves connecting to your server via SSH. This can seem like a daunting task, perhaps one you've never attempted before, so I'll take than into consideration as I explain the process. It's really not too difficult and hopefully the instructions will have you executing commands like a pro in short! First, you'll need a program that is capable of accessing your server via SSH. I use a program called putty that's freely available. You will use this program on your local computer, so download the version for your desktop/laptop's operating system.
Next, open (launch) putty.exe. Here's a screen shot of what it looks like:
picture of putty interface for connecting to a web server
Enter your server's IP address. The port should be 22, unless you've configured your SSH access to another port.
Next are a series of text you'll type into putty:
  • login as: username
  • password: password
  • enter: cd /etc/httpd/conf/
  • enter: pico httpd.conf
  • press: ctrl w
  • enter: youdomain.com
OK, let's pause here for a moment. What we've done so far is opened up putty, logged in, changed to the conf directory, opened httpd.conf, pressed ctrl w (control key and the w key), entered your domain, press enter. This should lead you to a screen that looks similar to this one:
editting httpd.conf in putty
What you are going to do here, is edit the line that looks like:
ServerAlias yourdomain.com
to:
ServerAlias: *.yourdomain.com yourdomain.com
You can accomplish this by using the arrows on your keyboard to place the curser to enter: *.yourdomain.com
One more illustration, to show what it looks like once added:
editting httpd.conf in putty
Take a look at the previous photo, then the one just above. You are just adding "*.yourdomain.com" to line two in the illustration, the line for "ServerAlias" .
Once you've entered that, press "ctrl x" to exit. Be sure to save your work, you will be prompted to do so.
OK, so with step one and two, you've configured your server to use a wildcard dns for your subdomains. Next, we'll look at some php code to pull it all together!

Getting your wildcard subdomain name with PHP

You really don't need a .htaccess file to use this in it's simplest form. Instead, we can simply trap for the subdomain in our index.php file. Of course, this assumes your are using one file, perhaps index.php, for your subdomain purposes, but if you've made it this far, you can probably figure out what's taking place.
The code:

$serverhost = explode('.',$_SERVER["HTTP_HOST"]);
$sub = $serverhost[0];
if ($sub = "www") {
$sub = "";
}

OK, here's what's taking place. You insert this code in your main php file and what it does is check to see if the subdomain portion of the domain (ie: thishere.yourdomain.com) is www. If so, it just nulls $sub, otherwise, $sub will contain your subdomain keyword. Now, you can check if ($sub > "") and take appropriate action with your code if a subdomain exists, to display a page based on that value.
So there you have it, using wildcard dns for subdomains on cpanel. Hopefully this will help you get going.

رابط على ديج