Implementing custom domains

Hi guys,

Just wondering how sites like tumblr or weebly are able to implement custom domains. For example when you first sign up you get a sub domain like “”. But they allow you the option of changing that to your own domain, provided that you supply the nameservers.

I just want to know what is going on behind the scenes…

(PS: sorry for a very basic question, I am fairly new to server administration and still trying to get my head around how DNS works).

Any help would be greatly appreciated. Many thanks!

You point the DNS record for your domain (A or CNAME) to tumblr’s servers. No different than pointing it at your own server.

Their web server gets the HTTP requests the same as if it was their domain.

All HTTP requests are pointed to the same document root; no virtual hosts or anything else domain specific.

Their code looks up what user has registered that hostname and shows their site.

Tumblr itself doesn’t have to do anything to support custom domains except store what domain is associated with what user in their database.

Thanks Dan for the help. That made it clearer for me. So just to confirm, the code that tumblr executes, would look something along the lines of (pseudo code):

// get the requested url (eg the custom domain)

// find in the database the user with the custom domain

// serve the page that is linked with that user

Would this be right or am I totally off the mark. Also could you direct me to any code samples that demonstrates this?

Thanks again.

Skuvar, take a look at how DNS works. I think that will answer most of your questions. As to the code, while I don’t have an actual example I can tell you it is easily done just by mapping the user’s domain to the site in the database. Once that is done you have numerous choices on how to implement the redirection itself based on the application your using.

Not sure how helpful that’ll be…

$domain = $_SERVER['HTTP_HOST'];

mysql_connect('localhost', 'username', 'password');
$result = mysql_query("SELECT id FROM users WHERE domain = '$domain'");
$row = mysql_fetch_assoc($result);


Many thanks for the help guys. I understand how it works now. :slight_smile:

Thanks again. Regards.