Question

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)

Was it helpful?

Solution

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

OTHER TIPS

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.

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top