2

Say I have a site hosted on example.com, now I want each of my registered user to get a personalized sub-domain, e.g Alice should normally gets alice.example.com as her sub-domain. While actually alice.example.com gets data from example.com/user/alice. How to implement this mechanism? I see lots of sites are working in this way. I wonder how they did it. Is it some kind of domain server configuration, web-server configuration, .htaccess rewrite or simply some tricks in the code?

If its redirecting, as I experimented, example.com and alice.example.com will get DIFFERENT IP addresses, if that, how rewrite works since when I request alice.example.com, the web browser takes me to a completely different site (IP address).

This has confused me for some time, I just can't figure it out myself, any help would be really appreciated.

[edit]: OK WAIT! I'm afraid I made a duplicate. Check this thread: Create subdomains on the fly with .htaccess (PHP)

Community
  • 1
  • 1
Shawn
  • 32,509
  • 17
  • 45
  • 74
  • Can't help you. But i gotta say it. Its a good Question. – Fábio Antunes Sep 26 '09 at 12:05
  • A word of advice, try asking this question at serverfault.com the Server guys are usually there. – Fábio Antunes Sep 26 '09 at 12:09
  • To me. Server fault right now is still kinda a small village. The other reason I put this question here was I wasn't sure if it's programming related or system admin stuff. Sorry to found that someone already asked this. See http://stackoverflow.com/questions/586129/create-subdomains-on-the-fly-with-htaccess-php – Shawn Sep 26 '09 at 12:17

3 Answers3

1

Basically you need two parts:

1) Your Nameserver needs to support wildcards. So you would map *.mydomain.com to a single server. This will cause alice.mydomain.com and bob.mydomain.com to all go to the same server.

2) Then your server software should be able to map the hostname (=alice.mydomain.com) to your application and pass in the "alice" part as a parameter.

Depending in what framework/server software you use this should be quite easy.

HTH Alex

Alex Duggleby
  • 7,948
  • 5
  • 37
  • 44
  • Thanks. I'm on goDaddy, wondering if their nameserver supports wildcards. – Shawn Sep 26 '09 at 12:06
  • @Shawn: their nameserver supports nothing that may aid you. Consider using nameserver of your hosting provider. – P Shved Sep 26 '09 at 12:36
  • goDaddy support suggested me to add a wildcard A record to point to my main domain's ip. Will that work? – Shawn Sep 27 '09 at 00:33
1

Is it some kind of domain server configuration, web-server configuration, .htaccess rewrite or simply some tricks in the code?

It's probably a combination of the several techniques.

First layer would probably be a Wildcard DNS record on a DNS level so that any sub-domain can be used.

Next, server configuration or application logic is used to separate the content based on the accessed sub-domain. This can be Apache Alias directive or a Rewrite rule or logic in your application code.

Marko
  • 3,499
  • 3
  • 24
  • 22
1

I can tell how I did this on pastebin.com

  • wildcard DNS makes *.pastebin.com go to a single IP
  • By default, if Apache can't find a matching vhost for a given domain, the first defined vhost picks it up (there are other ways of achieving this, this is just what I did).
  • So I ensure the pastebin site is the first vhost, and so then software then sees the requested hostname and acts on it accordingly to configure itself
Paul Dixon
  • 295,876
  • 54
  • 310
  • 348
  • Thanks. How did deal with www. and other none www. subs? I suppose ass *.mydomain.com suggests, www.mydomain.com should be excluded when doing the wildcard thing? – Shawn Sep 26 '09 at 12:21