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

Hello, I have two domains say for example and

I have set my web directory up to be like /var/www/ and /var/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 ServerName ServerAlias DocumentRoot /var/www/ ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined </VirtualHost>

<VirtualHost *:80> ServerAdmin ServerName ServerAlias DocumentRoot /var/www/ ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined </VirtualHost>

I have also ran these commands: a2ensite a2ensite service apache2 reload

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

When I load or it just redirects to 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?

Show comments

Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

Hi, I have the same problem. In apache2.conf I added

<Directory /var/www/test>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted

<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.
        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/test

        # 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
RewriteEngine on
RewriteCond %{SERVER_NAME}
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

I’ve created a ssl using sudo certbot certonly --apache for the domain

Also added the record to the DNS (in digitalocean).

When accessing it redirects to exist domain

The new site is enabled and found in /etc/apache2/sites-enabled.

How to trace it? Where’s my error?

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 while I was building the site. I have updated these now and waiting for them to propagate.


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>
DocumentRoot /var/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.