Creating Wildcard Subdomains + Controller/Dispatcher

Ever wanted to create an automatic system that will open subdomains for your customers? Or maybe you would like to control your subdomains without having to mess around with DNS and configuration files everytime, but have wildcard subdomains?

You’re in the right place. Continue reading!

If you don’t know what you’re doing – don’t blame me if your server gets all whacked. You should follow these instructions only if you feel comfortable with DNS and the Apache configuration file.

NOTE: Replace yourdomain.com with your actual domain, and 63.63.63.63 with the actual IP your site is using.

  1. Add a DNS entry to your site DNS zone: *.yourdomain.com. 14400 IN A 63.63.63.63I have cPanel, and I used the “Edit DNS Zone” tool to add this record. Very easy! :)
  2. Restart Named service. (/etc/init.d/named restart)
  3. At this point everything worked for me fine, and I didn’t have to follow this step. A lot of people DO advise to add the new alias to your ServerAlias under the site VirtualHost in the Apache configuration file (/etc/httpd/conf/httpd.conf) like this: ServerAlias www.yourdomain.com yourdomain.com *.yourdomain.com
    Your VirtualHost should looks something like this:
    <virtualhost 63.63.63.63>
    ServerAlias www.yourdomain.com yourdomain.com *.yourdomain.com
    ServerAdmin webmaster@yourdomain.com
    DocumentRoot /home/yourdomain/public_html
    BytesLog domlogs/yourdomain.com-bytes_log
    ServerName www.yourdomain.com
    <ifmodule mod_userdir.c>
    Userdir disabled
    Userdir enabled yourdomain_user
    </ifmodule>
    <ifmodule mod_php4.c>
    php_admin_value open_basedir "/home/yourdomain:/usr/lib/php:/usr/local/lib/php:/tmp"
    </ifmodule>
    User yourdomain
    Group yourdomain
    CustomLog domlogs/yourdomain.com combined
    ScriptAlias /cgi-bin/ /home/yourdomain/public_html/cgi-bin/
    </virtualhost>
  4. If you edited the httpd.conf file, save it, and restart the Apache service. (service httpd restart)

Alright! You can now try out your new wildcard subdomains, they are supposed to work now, and redirect any subdomain to the root of your website. You can now grab the subdomain in PHP, or use some .htaccess magic to redirect them into their special folders.

Here’s the code to paste into your .htaccess file to redirect subdomain calls into a specific directory:
RewriteEngine On
RewriteRule ^\.htaccess$ - [F]
RewriteCond %{HTTP_HOST} !^www\.example\.com?$
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com?$
RewriteRule ^$ /accounts/login.php [L]

Or if you would like to send the subdomain as a URL query parameter, replace the LAST RewriteRule above with:
RewriteRule ^(.*)$ /accounts/login.php?user=$1 [L]

Here is a PHP code snippet of how to capture the subdomain name:

<?php
   $server_host = explode('.', $_SERVER['HTTP_HOST']);
   $subdomain = strtolower($server_host[0]);
   echo 'Subdomain name is: ' . $subdomain;
?>

Some helpful links:
haalaaluu.com – Wildcard Virtual Host and htaccess Subdomain
photomatt.net – Wildcard DNS and Sub-domains

Comments: 4 Comments.
Comments
Comment from Kristina - May 12, 2006 at 00:43

I have a question for you…

First though,,,, I realy appreciate this post,, it is the first one in the line of many that makes some sense.
Good work…

Now here is what I have on my hands….

I have a hosting account with hostgator and have access to cPanel but no access to WHM…Not to mention that I can’t edit my http.conf file as I please. However, I spoke to their support and as the first response so far, they suggested that I use PHP to accomplish what I need by somehow passing the variables to cPanel…. So they have no problem with me finding the workaround for my needs.

No access to WHS,, therefore,,, I can’t EDIT DNS ZONE myself. I am pretty sure they would edit it for me but I would like to explain some things to myself first.

I have a situation where I want to accomplish this….

If I have:

http://www.mydomain.com/somefolder

I would like it to apear as:

somefolder.mydomain.com

Is what you are suggesting in your post a partial soluiton to what I am after???

That being…

If I edited (or if they edited) my DNS zone in WHM by simply changing the A record like this:

*.mydomain.com. 14400 IN A xx.xx.xx.xx

and then I use .htaccess file like this

RewriteEngine On
RewriteRule ^\.htaccess$ – [F]
RewriteCond %{HTTP_HOST} !^www\.mydomain\.com?$
RewriteCond %{HTTP_HOST} ^([^.]+)\.mydomain\.com?$
RewriteRule ^$ /subdomainfolder/index.html [L]

(how do I read out the “subdomainfolder” from a browser window call?)

Or,do I use PHP code in index.php as you sugested:

I should have the solution DONE??

Goodness I know I killed you with this post but I am almost desperate…
Plus I am not a savy PHP developer,,, worked the desktop stuff for too long?

Thank you :)

Comment from Kristina - May 12, 2006 at 12:42

Just to add,,,, it works,,, I managed to get the provider to wildcard my domain :) and I am using mod_rewrite to access subdomains on the fly

Comment from justRafi - May 12, 2006 at 13:26

Alrightttt! :) good for you!
What are you building? Must be something interesting.

Anyways, good luck on the road ahead..

Comment from Nl2QL - May 19, 2006 at 13:52

Hello,

I’m using your rule but it doesn’t go to directory ( name of subdomain ), how can i make this?? I need to load directory by subdomain name…

RewriteEngine On
RewriteRule ^\.htaccess$ – [F]
RewriteCond %{HTTP_HOST} !^www\.example\.com?$
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com?$
RewriteRule ^$ /$1 [L]

is last line correct, or what i need to use?? Thank you..