Apache vhost issue, two domains, second redirects to first ?

January 14, 2017 10.8k views
Apache Ubuntu 16.04

I have two domains say for example site1.co.uk and site2.uk

I have set my web directory up to be like /var/www/site1.co.uk/www and /var/www/site2.uk/www.

For the vhost conf file for each domain, I have pointed the docroot to the nessesary directory.

Here is an example of my config files:


<VirtualHost *:80>
ServerAdmin email@site1.co.uk
ServerName site1.co.uk
ServerAlias www.site1.co.uk
DocumentRoot /var/www/site1.co.uk/www
ErrorLog ${APACHELOGDIR}/error.log
CustomLog ${APACHELOGDIR}/access.log combined


<VirtualHost *:80>
ServerAdmin email@site2.uk
ServerName site2.uk
ServerAlias www.site2.uk
DocumentRoot /var/www/site2.uk/www
ErrorLog ${APACHELOGDIR}/error.log
CustomLog ${APACHELOGDIR}/access.log combined

I have also ran these commands:
a2ensite site1.co.uk.conf
a2ensite site2.uk.conf
service apache2 reload

Now when I enter site1.co.uk or www.site1.co.uk in my browser. No problem. The website loads, the correct doc root loads etc.

When I load site2.uk or www.site2.uk it just redirects to www.site1.co.uk. I have installed a headers extension on my browser and it is telling me it gives Status: HTTP/1.1 301 Moved Permanently.

Can anyone give me pointers how to correct this issue?

  • It looks good, but did you tried restart instead of reload? Just to be sure:

    • sudo systemctl restart apache2

    If you use Ubuntu 14.04 or older:

    • sudo service apache2 restart
  • Hi , yeah i have tried both of them . With and without sudo . I have also a2dissite all the vhosts and then a2ensite them back again before restarting.

    From my understanding Apache routes to the default vhost a sort of catch all . But I have a specific vhost for site2.uk so don’t understand why it is redirecting me to the default

4 Answers


Your VirtualHost configurations appear to be correct, though you said that what you provided are just examples. Can you post the full configurations?

Also, have you checked for an .htaccess in the root of either site? If they exist, please post them as well. With a valid, working .htaccess file, it’s possible for a redirect to be defined, so we should check there as well.

  • There are no .Htaccess files and the below is the conf files

    aparkermedia.uk a.k.a site1.co.uk (the working one)

    <VirtualHost *:80>
    ServerAdmin arron@aparkermedia.uk
    ServerName aparkermedia.uk ServerAlias www.aparkermedia.uk
    DocumentRoot /var/www/aparker media.uk/www/ ErrorLog ${APACHELOGDIR}/error.log
    CustomLog ${APACHELOGDIR}/access.log combined

    Kaiser Knight.co.uk a.K.a site2.uk

    <VirtualHost *:80>
    ServerAdmin arron@aparkermedia.uk
    ServerName kaiserknight.co.uk ServerAlias www.kaiserknight.co.uk
    DocumentRoot /var/www/kaiserknight.co.uk/www/ ErrorLog ${APACHELOGDIR}/error.log
    CustomLog ${APACHELOGDIR}/access.log combined


As long as the VirtualHosts actually look like the below for each domain – i.e. each definition is set on a new line.

<VirtualHost *:80>
ServerAdmin arron@aparkermedia.uk
ServerName kaiserknight.co.uk
ServerAlias www.kaiserknight.co.uk
DocumentRoot /var/www/kaiserknight.co.uk/www/
ErrorLog ${APACHELOGDIR}/error.log
CustomLog ${APACHELOGDIR}/access.log combined

Then your Apache configuration looks good. So let’s look elsewhere…

When performing a DNS lookup on your domain names, the only valid entry I see, other than your MX, is a single A entry for each domain.

When looking at your VirtualHost configuration, you’re setting a ServerAlias that defines the www. access point for each domain, but you’re missing a DNS entry on both domains that points www. to your domain. Since the DNS record isn’t set, Apache will handle it the best way it knows how, which may or may not be correct.

So what I would recommend doing is adding an A entry to each domains’ DNS records. The A entry should point to the same IP that your domain points to (i.e. the public IP of the Droplet).

So www should be an A entry that points to in your case. Since it looks like your using DigitalOcean for your DNS, you can make these changes through their control panel and the updates should take effect pretty quickly. If they don’t, you may need to clear your browsers cache and then try to access your domains again.

If the DNS updates do not fix the issue, then we’d need to check the logs and potentially even the actual configuration for Apache.

  • @jtittle1 - I’m a bit late to this, but thanks for your response to @arronparker93 .

    I’m having a similar problem to @arronparker93. My first site (arcticdh.ucalgary.ca) is working fine (index.html showing in browser), but my second site (uak.ucalgary.ca) just hangs and times out in the browser. Nothing I try has fixed this.

    I was wondering, do both my first and second site need to be an A record? Or should it be ok that my second site is a CNAME record.

    I’ve been working on my problem for days and trawled through all the forum threads I could find…argh! Any help would be greatly appreciated!

    My httpd.conf has these blocks:
    <VirtualHost *:80>
    ServerName arcticdh.ucalgary.ca
    DocumentRoot /var/www/html/arcticdh
    ErrorLog /var/www/html/arcticdh/error.log
    CustomLog /var/www/html/arcticdh/requests.log combined

    <VirtualHost *:80>
    ServerName uak.ucalgary.ca
    DocumentRoot /var/www/html/uak
    ErrorLog /var/www/html/uak/error.log
    CustomLog /var/www/html/uak/requests.log combined

Thanks all for your help, I hate to admit this but total noob mistake on my part. I hadn’t updated the nameservers of my domain at my registrar. They were still pointing to godaddy which I had set up to redirect to aparkermedia.uk while I was building the site. I have updated these now and waiting for them to propagate.

Have another answer? Share your knowledge.