0

A user gets a subdomain in my application, customer.myapp.com. Since users will want to use their own custom domain I'll expect them to setup a CNAME record pointing customer.com to customer.myapp.com. I was hoping to avoid creating a vhost record for every customer but I think that's what I'm going to have to do. I can't quite find a definitive answer, so am I right in assuming that? If so what is the best way to go about this? Can my application programmatically edit/add vhosts? I'd like to be hands off when a customer signs up for my service. How do services like Shopify and Tumblr handle this?

6
  • Why do you think you need a vhost for every domain in use? – AD7six May 11 '15 at 11:45
  • You're right, I don't need a vhost for every customer, but I at least need a ServerAlias for every customer. What I'm trying to avoid is manually updating Apache every time a customer signs up. – Ryan Arneson May 11 '15 at 11:56
  • 1
    Don't bother doing this in the webserver configuration at all, simply handle this in the application. en.wikipedia.org/wiki/Multitenancy Let the default virtual host catch any (sub)domain pointed to your webserver and than in your code you use the HTTP HOST header to map the domain name used to a specific customer . That way you also don't have to deploy a new instance of your application code for each customer. – HBruijn May 11 '15 at 12:09
  • I think you may have just pointed me in the right direction. I'm on a VM with multiple vhosts. The vhost for this site (myapp.com) handles wildcard subdomains (*.myapp.com) via a ServerAlias and my appl handles multi-tenancy. What I was hoping was that when apache sees a request from a CNAME that is pointing to one of these subdomains (customer.com --> customer.myapp.com) that it would route appropriately to the vhost with a ServerAlias of *.myapp.com. This doesn't seem to be the case. What you're saying is if I use the default vhost to route these requests it should handle it appropriately? – Ryan Arneson May 11 '15 at 14:21
  • @HBrujin: Add an answer and I'll accept it. Letting my default vhost catch all requests is what worked. My problem was trying to use a vhost for my sites domain so my customer's CNAME records were still getting routed through the default and that's why I was seeing "It works!". – Ryan Arneson May 12 '15 at 14:13
1

Perhaps you can have some interest in mod_vhost_alias http://httpd.apache.org/docs/2.2/mod/mod_vhost_alias.html. Instead of setting up each domain you set up a instruction telling apache to search for DocumentRoot of a domain in a path containing that domain name.

0

I've seen it done in a company I used to work for, but it wasn't set up by me. Expanding on Florian's answer a little, there's a blog post which covers how to do this and explains how it works: https://muffinresearch.co.uk/redirecting-subdomains-to-directories-in-apache/

1
  • Whilst this may theoretically answer the question, please provide context around links so others will have some idea what it is and why it’s there. If possible summarise or quote the most relevant part of an important link, in case the target site is unreachable or goes permanently offline. – HBruijn May 11 '15 at 12:40
0

You can either use the ServerAlias directory (it's an addon to ServerName). It takes a list of 'other' names for a vhost.

The harder-to-implement way is using mod_rewrite, which can also used for vhosts. It can be a better option if you go to the 100s or 1000s of names for a vhost. But it is rarely used. It comes with the big bonus of not needed apache reloads. Some documention is here: http://httpd.apache.org/docs/2.2/rewrite/vhosts.html

Edit: I did not think of or mention the rewritemap option: http://httpd.apache.org/docs/2.4/rewrite/rewritemap.html

I hope that the 'big services' wrote something more efficient for their use cases!

1
  • I'm using a ServerAlias right now, but I'm trying to avoid that since I'll need to manually add that for each customer sign up. – Ryan Arneson May 11 '15 at 11:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.