views:

103

answers:

3

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: http://stackoverflow.com/questions/586129/create-subdomains-on-the-fly-with-htaccess-php

+1  A: 

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

AlexDuggleby
Thanks. I'm on goDaddy, wondering if their nameserver supports wildcards.
Shawn
@Shawn: their nameserver supports nothing that may aid you. Consider using nameserver of your hosting provider.
Pavel Shved
goDaddy support suggested me to add a wildcard A record to point to my main domain's ip. Will that work?
Shawn
that should do the trick
AlexDuggleby
+1  A: 

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
+1  A: 

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
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