Baadier
By:
Baadier

Sub-domain routing to different top level domain

February 11, 2015 1.5k views

Hi,

I have 2 sites hosted on my LAMP droplet.

The subdomain.domainX.com site is routing to domainY.io

I have 4 configuration files in my /etc/apache2/sites-available: default, default-ssl.conf, subdomainX.com.conf, domainY.io.conf

This is what the configuration file for subdomainX.com.conf looks like:

<VirtualHost *:80>
    # The ServerName directive sets the request scheme, hostname and port that
    # the server uses to identify itself. This is used when creating
    # redirection URLs. In the context of virtual hosts, the ServerName
    # specifies what hostname must appear in the request's Host: header to
    # match this virtual host. For the default virtual host (this file) this
    # value is not decisive as it is used as a last resort host regardless.
    # However, you must set it for any further virtual host explicitly.
    ServerName www.subdomain.domainX.com
    ServerAlias subdomain.domainX.com

    ServerAdmin contact@domainX.com
    DocumentRoot /var/www/domainX

    <Directory />
        Options FollowSymLinks
        AllowOverride None
    </Directory>
    <Directory /var/www/domainX>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Order allow,deny
        allow from all
    </Directory>
    # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
    # error, crit, alert, emerg.
    # It is also possible to configure the loglevel for particular
    # modules, e.g.
    #LogLevel info ssl:warn

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

    # For most configuration files from conf-available/, which are
    # enabled or disabled at a global level, it is possible to
    # include a line for only one particular virtual host. For example the
    # following line enables the CGI configuration for this host only
    # after it has been globally disabled with "a2disconf".
    #Include conf-available/serve-cgi-bin.conf
</VirtualHost>

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

and the configuration file for domainY.io

NameVirtualHost *
<VirtualHost *:80>
    # The ServerName directive sets the request scheme, hostname and port that
    # the server uses to identify itself. This is used when creating
    # redirection URLs. In the context of virtual hosts, the ServerName
    # specifies what hostname must appear in the request's Host: header to
    # match this virtual host. For the default virtual host (this file) this
    # value is not decisive as it is used as a last resort host regardless.
    # However, you must set it for any further virtual host explicitly.
    ServerName www.domainY.io
    ServerAlias domainY.io

    ServerAdmin contact@domainY.io
    DocumentRoot /var/www/domainY/domainY.io

    <Directory />
        Options FollowSymLinks
        AllowOverride None
    </Directory>
    <Directory /var/www/domainY/domainY.io>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride All
        Order allow,deny
        allow from all
    </Directory>
    # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
    # error, crit, alert, emerg.
    # It is also possible to configure the loglevel for particular
    # modules, e.g.
    #LogLevel info ssl:warn

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

    # For most configuration files from conf-available/, which are
    # enabled or disabled at a global level, it is possible to
    # include a line for only one particular virtual host. For example the
    # following line enables the CGI configuration for this host only
    # after it has been globally disabled with "a2disconf".
    #Include conf-available/serve-cgi-bin.conf
</VirtualHost>

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

This feels like it should be really simple and its likely that I'm being really stupid here and just missing something trivial but I've been stuck at it for a few hours already.

Thanks for the help.

2 comments
  • Let me see if I get it right, you need to redirect subdomain.domainX.com to domainY.com?
    if is this the case, why you setup a subdomain.domainX.com site on apache? if you do that apache will answer for requests to subdomain.domainX.com. Unless you are using some html redirect on that site to forward visitors to domainY.com there is no reason to have a site for subdomain.domainX.com running on apache.

    Go to your DNS manager (wherever you have it hosted and create a cname record on domainX.com like this:

    cname subdomain.domainX.com -> domainY.com

    That is all you need to redirect a subdomain to another domain.

  • Thanks for helping out.

    I'm running software on the subdomain on DigitalOcean. I've managed to get the DNS working without hassle but the site was still resolving to the other site.

    I eventually just scrapped it and spun up a new droplet and took a snapshot when I got it the way I wanted.

    Thanks again.

Be the first one to answer this question.