views:

91

answers:

1

When one signs up for Blogger or WordPress, one gets very own subdomain that works instantly. How can do like that given I have my own VPS/VDS/Dedicated server?

+5  A: 

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
Heinzi
+1 Thanks Heinzi. Could you please elaborate more and give examples? I'd appreciate your help very much!
Viet
@Viet: I've extended the answer. If you need more information (e.g. about some specific technology), just ask.
Heinzi
Viet
I would prefer rewrite than checking the host part. How this can be done efficiently? Thanks!
Viet
I've added two links on how to do this with Apache's mod_rewrite.
Heinzi
+1 Thanks again! Those links with examples ensure that there will be no redirects from subdomains isn't it? Because I need http://sub.domain.com point to http://domain.com/sub without a redirection.
Viet
And if I don't want a browser redirection (301/302), what should I do? Thanks in advance!
Viet
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 (http://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.
Heinzi
+1 I'll test that out. Thanks again! I wish I could vote more!
Viet