-2

I have the domain example.com and have set up the Ubuntu Apache server in a way that it points any subdomain to a directory with the same name.

<VirtualHost *:80>
    ServerAlias *.example.com
    VirtualDocumentRoot /var/www/html/%1
</VirtualHost>

So subdomain.example.com will point to example.com/subdomain.

I am trying to allow users point their own custom domain to a subdomain of their choice, how could I achieve this and what records would be required and what would have to be done to the Virtual Host?

I have tried the following record:

  • sampledomain.example (CNAME record) - @ --> sample.example.com
  • sampledomain.example (A record) - @ --> (IP Address of example.com)

I have also tried using www for the CNAME along with removing the A record.

sample.example.com successfully points to example.com/sample. However, sampledomain.example acts like the root site for some reason.

So if I visit sampledomain.example/sample, I will get the contents of example.com/sample rather than pointing to sample.example.com.

How could I sort this out? Which records would be needed and what could be done to the Virtual Host?

I used a CNAME record so that I can retrieve the custom domain name via PHP when the user visits that - is the CNAME required?

Patrick Mevzek
  • 9,273
  • 7
  • 29
  • 42
Osman
  • 97
  • 1

1 Answers1

1

If you are OK with doing this manually on request for your customer, this is easy. All you have to do is to add ServerAlias sampledomain.com to the vhost configuration and reload the web server config.

However, if you want to have your customers doing this themselves in some kind of customer control interface, this is not easily achievable due to the way virtual hosts on a web server work. Here is why:

If you have any DNS record (regardless if A or CNAME) mapping sampledomain.com to your web server, the browser will send a request to your servers IP address with the HTTP Host header set to sampledomain.com. The web server will use this header to decide which virtual host is supposed to handle this and if sampledomain.com is neither a vhost name nor alias, the request will be answered by the default vhost as you have experienced.

To make this possible, you need a way for your customers to configure aliases for their vhost. With Apache, this is not easy but in theory you can read this from a database, as explained here (I've never tried this, so I've no clue if this works).

Also, things like VirtualMin might be helpful in such a case.

Sven
  • 97,248
  • 13
  • 177
  • 225