Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to allow customer subdomain sites to use their own domain

I have a rails app where customers sign up and get a custom subdomain site/url like this:

  • customer1.myapp.com
  • customer2.myapp.com
  • customer3.myapp.com

What are the steps I need to take to allow a customer to use their own registered domain name such that their domain points to my app?

So, in the example above, if "customer1" owns "customer1.com" - how can I setup my app so that any requests to "customer1.com" are sent to "customer1.myapp.com"? Also, what would my customer need to do on his end?

Thanks.

like image 665
Jim Jones Avatar asked Jul 15 '10 14:07

Jim Jones


People also ask

Can I point a domain name to a subdomain?

It is possible to point a subdomain to another domain name with the help of CNAME record or ALIAS record. The important difference is that ALIAS can coexist with other records on that name. ALIAS record can also be used if you wish to alias the root domain to another service (which you cannot do with a CNAME record).

Can domain and subdomain on different servers?

Yes, It's possible. For Example. You would either Contact your Host to set this, or if you have the ability to point DNS your self then do so. Just make sure that the site you want to serve on that subdomain exists on the server that it's pointing to.


1 Answers

Your customer is going to need to set up DNS For their domain to point it, or part of it, to your address. This can be tricky to coordinate, especially if the address of the server you're hosting the service on can change from time to time. It's a lot easier to route a customer's subdomain to your subdomain.

You'll also need a lookup table that maps a customer's domain to a customer's account. That's typically expressed as something like this:

before_filter :load_customer_from_host

def load_customer_from_host
  # Strip "www." from host name to search only by domain.
  hostname = request.host.sub(/^www\./, '')

  @customer = Customer.find_by_host!(hostname)
rescue ActiveRecord::RecordNotFound
  render(:partial => 'customer_not_found', :layout => 'application', :status => :not_found)
end

That presumes you have a Customer model with a 'host' field set with something like 'customer1.myapp.com' or 'customer1.com', whatever matches the host field.

When you set up your app, you will need to have a virtual host configuration that responds to all arbitrary domain names. This is easy to do if it is the only site hosted, since that is the default behaviour. If you're doing this on shared hosting you may have to configure an alias for each customer domain specifically, which can be a nuisance.

like image 174
tadman Avatar answered Nov 15 '22 20:11

tadman