

Ask HN: Mapping Customer's Domain/Subdomain to my site - dannyr

I believe some of you have done this.<p>I have a customer (customer1.com) who wants to map a subdomain or domain to its account on my site (mysite.com). Example is sub.customer1.com to www.mysite.com/customer1 or customer1.mysite.com.<p>How do I do this? Posterous and Weebly are doing this to their customers.<p>Thanks.
======
jacquesm
In your DNS configuration you have to put a line like this:

    
    
       *.DOMAIN.com.               IN      A       YOURIP
    

Where DOMAIN and YOURIP have to be replaced by the relevant data. (the
asterisk character goes into column '0', the separators between the fields are
tabs).

Then, when that works and whateveryoutype.domain.com maps to YOURIP you have
to set up your webserver to handle this.

I'm going to assume that you use apache, for other webservers you'll have to
do something else.

In the VirtualHost section, just under 'ServerName' for your webserver add
this line:

    
    
        ServerAlias *.DOMAIN.com
    

And restart the server.

Now in your language of choice you need to fish out the hostname, and load a
database record with the specifics of that subdomain.

In php you would need to access:

    
    
        $_SERVER['HTTP_HOST']
    

good luck!

------
brk
customer1.mysite.com is easier to do.

First, you setup a DNS record:

customers.mysite.com IN A 12.34.56.78

Then, your customers setup CNAME records:

sub.customer1.com IN CNAME customers.mysite.com

Next, depending on your webserver (going to assume Apache), you setup
virthosts:

<VirtualHost _:80 > ServerName customer1.mysite.com

    
    
       (etc...)

</VirtualHost> <VirtualHost _:80> ServerName customer2.mysite.com

    
    
       (etc...)

</VirtualHost>

The reason you setup the customers.mysite.com record and have EVERYONE CNAME
to that is because when you eventually change servers/hosts/IP addresses you
simply update that single record (which is under your control) and then
everything else follows. Same logic for when you eventually need to insert a
load balancer or an haproxy auto-failover maintenance page.

------
gsmaverick
You probably will have to deal with it in your scripting language of choice.
You find the sub-domain and have that link to something in your database to
link it to their account/data.

------
there
depends highly on your software stack that you're using to handle requests.

for starters you'll need to define a wildcard in your domain so that
*.mysite.com all resolves to the same ip.

then you may have to configure your web server with the same wildcard, then
once it trickles down to your application, you'll probably have to look at the
SERVER_NAME variable passed by the web server.

~~~
dtby
If there is only one company requiring this functionality, you don't need a
wild card.

In the customer's DNS zone file:

mysite.customer1.com. IN CNAME customer1.mysite.com.

In your DNS zone file:

customer1.mysite.com. IN CNAME www.mysite.com.

Then, you'll need to decide how to configure the web server to provide the
proper resource on requests for "customer1.mysite.com" In simple static
situations, this could be as simple as a virtual host... more complex dynamic
situations may require a redirector.

~~~
dannyr
It will be for all my customers.

I just checked and my host supports wildcard domains.

Posterous is doing the same thing. They are using an IP address to map the DNS
though. I wonder if you can use a domain (e.g. ns1.mysite.com) in case the IP
address changes. <http://posterous.com/help/custom_domain>

