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:
- Create a wildcard domain in DNS (i.e., resolving whatever.yourdomain.example returns your IP),
- create a default virtual host in your web server and
- check the URL in your application.
How to do this depends on what technology you use. Let me give you some examples:
- How to set up a wildcard domain in BIND and in Windows Server DNS.
- 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.
- 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
2010-02-03 07:25:28
+1 Thanks Heinzi. Could you please elaborate more and give examples? I'd appreciate your help very much!
Viet
2010-02-03 07:45:19
@Viet: I've extended the answer. If you need more information (e.g. about some specific technology), just ask.
Heinzi
2010-02-03 08:05:54
Viet
2010-02-03 08:09:16
I would prefer rewrite than checking the host part. How this can be done efficiently? Thanks!
Viet
2010-02-03 08:10:22
I've added two links on how to do this with Apache's mod_rewrite.
Heinzi
2010-02-03 08:15:52
+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
2010-02-03 08:20:24
And if I don't want a browser redirection (301/302), what should I do? Thanks in advance!
Viet
2010-02-03 08:23:57
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
2010-02-03 08:39:46
+1 I'll test that out. Thanks again! I wish I could vote more!
Viet
2010-02-03 08:46:59