How do I get personalized subdomains for users and make them work?

Let's say I have a website hosted on example.com, now I want each of my registered users to get a personalized subdomain, for example Alice usually gets alice.example.com as her subdomain. Although alice.example.com is actually getting data from example.com/user/alice. How to implement this mechanism? I see that many sites work this way. I wonder how they did it. Is it some kind of domain server config, webserver config, .htaccess rewrite or just some tricks in the code?

If redirecting it like I experimented with example.com and alice.example.com get DIFFERENT IP addresses, if that's how the rewrite works since I ask for alice.example.com the web browser completely repels me to another website (IP address).

This confused me a little, I just can't figure it out, any help would be much appreciated.

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

+2


source to share


3 answers


You basically need two parts:

1) Your Nameserver must support wildcards. This way, you could map * .mydomain.com to one server. This will cause alice.mydomain.com and bob.mydomain.com to 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 the "alice" part as a parameter.



Depending on what software / server software you are using, this should be pretty straightforward.

NTN Alex

+1


source


Is it some kind of domain server config, webserver setup, htaccess rewrite or just some tricks in the code?

This is probably a combination of several methods.



The first level will probably be a Wildcard DNS record at the DNS level so that any subdomain can.

Server configuration or application logic is then used to split the content based on the available subdomain. This can be an Apache Alias โ€‹โ€‹directive or a Rewrite rule, or logic in your application code.

+1


source


I can tell how I did it at pastebin.com

  • wildcard DNS server * * .pastebin.com goes to one IP address
  • By default, if Apache cannot find a suitable vhost for a given domain, the first specific vhost selects it (there are other ways to achieve this, this is exactly what I did).
  • So I guarantee the pastebin site is the first vhost and then the software then sees the requested hostname and acts on it accordingly to set itself up
+1


source







All Articles