Creating subdomains on the fly
Asked Answered
N

2

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?

Ns answered 3/2, 2010 at 7:23 Comment(1)
Wonderful question. I never knew this is possible.Strumpet
H
10

In a nutshell:

  1. Create a wildcard domain in DNS (i.e., resolving whatever.yourdomain.example returns your IP),
  2. create a default virtual host in your web server and
  3. check the URL in your application.

How to do this depends on what technology you use. Let me give you some examples:

  1. How to set up a wildcard domain in BIND and in Windows Server DNS.
  2. To create a default virtual host, you just need to create a web server without a host entry in IIS. In Apache, the first virtual host listed in the configuration file becomes the default host.
  3. Here, you can either (a) rewrite the URL depending on the domain (i.e., converting the subdomain into a parameter in the URL, example for ASP.NET, examples for Apache with mod_rewrite: Link1, Link2), or (b) just have a look at the host part of the URL (e.g. Request.Url in ASP.NET).

Addition by bortzmeyer (sorry for overwriting your edit, there was an edit conflict):

The syntax for a wildcard, in the usual DNS zone file format (described in RFC 1035 and implemented in BIND, nsd and may be others) is with a star:

 *   IN    A   198.51.100.3
Hobbism answered 3/2, 2010 at 7:25 Comment(5)
@Viet: I've extended the answer. If you need more information (e.g. about some specific technology), just ask.Hobbism
I would prefer rewrite than checking the host part. How this can be done efficiently? Thanks!Ns
I've added two links on how to do this with Apache's mod_rewrite.Hobbism
And if I don't want a browser redirection (301/302), what should I do? Thanks in advance!Ns
I didn't test the examples, but, as far as I can tell, they redirect only on the server side, i.e., there is no 301/302 sent to the client. According to the docs (httpd.apache.org/docs/1.3/mod/mod_rewrite.html), a client-side redirect is only performed when the right-hand side of the redirect starts with http://someOtherHost/ or the [R] flag is specified.Hobbism
Z
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.

Zealand answered 2/12, 2012 at 20:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.