How can I point an external domain to a subdomain?

March 20, 2018 2.5k views
Apache DNS PHP Ubuntu Ubuntu 16.04


I have the domain which I am trying to point to which in turn points to

I currently have this in my Virtual Host:

ServerAlias *
VirtualDocumentRoot /var/www/html/%1

I have tried creating a CNAME record for which points to, however for some reason it points to the root site

How could I allow users to directly point custom domains to subdomains of my site? What would have to be done to my Virtual Host/which records would be required?

Subdomains actually work as intended to since they point to the directories containing the same name. However, any custom domains pointed to any subdomain just points to the root site rather than pointing to the subdomain.

I would appreciate any help with this.

Thank you!

1 Answer

You are kind of on the right track as you do want the DNS for and to point to the same host which is However DNS (CNAME) does not take the URI into consideration. This is why ends up being directed to and not

To accomplish what you are looking for is to add some rewrite rule that change your domain to either or to

Some reference material to get you started

Be careful and take into consideration on mapping your domains to not create redirect loops or to many redirects.

  • Hi,

    Thanks for your response! Is there a way to do this dynamically though however?

    For example, I could create a PHP file with keys that point user custom domains to specific sub domains.

    Is there a way to have a rewrite rule that:

    • Grabs the current URL
    • Sends that URL to the PHP file and then points to whatever the PHP file returns?

    Thanks again!

Have another answer? Share your knowledge.