Alex Rivera | Logout

Creating subdomains on the fly

Asked 2010-02-03T07:23:32.793
10

When one signs up for Blogger or WordPress, one gets their very own sub-domain that works instantly. How can I achieve the same, given that I have my own VPS/VDS/Dedicated server?

Edit
Report

1 Answer

2

For those, who are laymen to all this A and CNAME things, there's a very simple solution and works with Shared Hosting:

Simply go to your cpanel and add a subdomain with *

For example, if your domain is called abc.com, you can add * and select/enter the sub-directory as a root to this. When you save, it will add *.abc.com in your sub-domains table and will add all necessary A records to your zonefile.

When you hit "any".abc.com in your browser, server will land you to the specified location (the sub-directory you mentioned).

Additionally, to handle all (any) sub-domain for specific redirection, you can use a .htaccess in that sub-directory to handle all incoming subdomain requests.

A working .htaccess example is as follows:

<IfModule mod_rewrite.c>

RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST} ^(^.*)\.abc\.com
RewriteRule (.*)  handler.php?user=%1&%{QUERY_STRING}

</IfModule>

The handler.php (code below) simply displays a welcome message with sub-domain name and all query string in the URL:

$user = $_REQUEST["user"];
print_r($_REQUEST);
echo "Welcome {$user}";

Hope this helps.

answered 2012-12-02T20:24:48.317

Your Answer