Question

Multiple vhosts all redirecting to one

Hello, my skills with Linux/Apache are pretty novice but I’ve setup many basic web servers over the years and have quite a bit of experience creating vhost files and deploying sites. I have run into a problem I’ve never experienced and I just cannot figure out a solution.

I have a Wordpress blog/small ecommerce site that I run on a DigitalOcean droplet. A friend of mine initially set it up a few years ago because I needed SSL and I wasn’t familiar with how to do that. Anyway, he got it setup and working just fine. This past week I wanted to add another site to the server. I created a new vhost file in sites-available, enabled it, reloaded/restarted Apache and tried to hit the URL. Whenever I go to the URL of the new site it just redirects to the original site.

Myself and another person spent a couple hours last night trying to figure out what was going on and, on a whim, I just decided to combine the two vhost files and it started working. That was late last night and I went to bed. Started working on it again this morning and it’s again redirecting to the original site. The only odd thing I noticed about the original setup is that there are two vhosts and both are enabled. I’ll put contents of each vhost below. Any help is greatly appreciated. Thank you.

Original Site: Vhost 1

<VirtualHost *:80>         
    ServerName originalsite.com 
    DocumentRoot /var/www/originalsite 
    
    RewriteEngine On 
    RewriteCond %{HTTP_HOST} ^(.*)$ [NC] 
    RewriteRule ^(.*)$ http://www\.originalsite\.com$1 [R=permanent,L] 
    RewriteCond %{SERVER_NAME} =originalsite.com 
    RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent] 
</VirtualHost> 

<VirtualHost *:80> 
    ServerName www.originalsite.com 
    DocumentRoot /var/www/originalsite 
    <Directory /var/www/originalsite> 
        Options Indexes FollowSymLinks MultiViews 
        AllowOverride All 
    </Directory> 
    CustomLog /http-logs/orig.log combined 
    ErrorLog /http-logs/orig.err.log 
    # Possible values include: debug, info, notice, warn, error, crit, 
    # alert, emerg. 
    LogLevel warn 
    RewriteEngine on 
    RewriteCond %{SERVER_NAME} =www.originalsite.com 
    RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent] 
</VirtualHost>

Original Site: Vhost 2

<IfModule mod_ssl.c>
    <VirtualHost *:443>         
        ServerName originalsite.com 
        DocumentRoot /var/www/originalsite 
        
        RewriteEngine On 
        RewriteCond %{HTTP_HOST} ^(.*)$ [NC] 
        RewriteRule ^(.*)$ http://www\.originalsite\.com$1 [R=permanent,L] 
        SSLCertificateFile /etc/letsencrypt/live/originalsite.com/fullchain.pem 
        SSLCertificateKeyFile /etc/letsencrypt/live/originalsite.com/privkey.pem 
        Include /etc/letsencrypt/options-ssl-apache.conf 
    </VirtualHost> 
</IfModule> 
<IfModule mod_ssl.c> 
    <VirtualHost *:443> 
        ServerName www.originalsite.com 
        DocumentRoot /var/www/originalsite 
        <Directory /var/www/originalsite> 
            Options Indexes FollowSymLinks MultiViews 
            AllowOverride All 
        </Directory> 
        CustomLog /http-logs/orig.log combined 
        ErrorLog /http-logs/orig.err.log 
        # Possible values include: debug, info, notice, warn, error, crit, 
        # alert, emerg. LogLevel warn 
        
        SSLCertificateFile /etc/letsencrypt/live/originalsite.com/fullchain.pem 
        SSLCertificateKeyFile /etc/letsencrypt/live/originalsite.com/privkey.pem 
        Include /etc/letsencrypt/options-ssl-apache.conf 
    </VirtualHost> 
</IfModule>

Second Site (redirecting to original)

<VirtualHost *:80>
    ServerName www.second.com 
    DocumentRoot /var/www/second 
    <Directory /var/www/second> 
        Options Indexes FollowSymLinks MultiViews 
        AllowOverride All 
    </Directory> 
    CustomLog /http-logs/second.log combined 
    ErrorLog /http-logs/second.err.log 
    # Possible values include: debug, info, notice, warn, error, crit, 
    # alert, emerg. LogLevel warn 
</VirtualHost>

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.

KFSys
Site Moderator
Site Moderator badge
August 30, 2023

Heya,

It seems you have a few issues that might be contributing to the problem:

  1. Overlap of <VirtualHost *:80> Blocks: Your originalsite.com configurations have multiple overlapping blocks that can confuse Apache on how to handle requests.

  2. RewriteRule and RewriteCond:

    • The Rewrite conditions/rules in the first vhost for originalsite.com will redirect all traffic to www.originalsite.com and further to HTTPS (using the condition %{SERVER_NAME} =originalsite.com). This might catch requests for other sites as well and redirect them.
  3. Missing HTTPS for the Second Site: You have no HTTPS configuration for second.com, which might lead to issues if someone tries to access it over HTTPS.

  4. Default Virtual Host: If Apache can’t find a match for the domain in the request, it serves the first <VirtualHost> block it comes across. This might also be causing the redirection.

Let’s address these issues:

  1. Consolidate VirtualHosts for originalsite.com:

    Combine the configurations for originalsite.com and www.originalsite.com under port 80:

<VirtualHost *:80>
    ServerName originalsite.com
    ServerAlias www.originalsite.com
    DocumentRoot /var/www/originalsite
    
    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^originalsite\.com$ [NC]
    RewriteRule ^(.*)$ http://www\.originalsite\.com$1 [R=permanent,L]
    
    RewriteCond %{HTTPS} off
    RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=permanent]
</VirtualHost>
  1. Setup the Second Site:

    Make sure that you have enabled the vhost for second.com:

sudo a2ensite second.com.conf`
Also, check that there is no conflicting `ServerName` or `ServerAlias` in other configurations.
  1. Add HTTPS Configuration for Second Site:

    Ensure that second.com also has an HTTPS (port 443) <VirtualHost> block similar to originalsite.com.

<IfModule mod_ssl.c>
    <VirtualHost *:443>
        ServerName second.com
        DocumentRoot /var/www/second
        
        # ... SSL configurations ...
    </VirtualHost>
</IfModule>

Ensure No Other Conflicting Configurations:

Check other configurations under sites-available and sites-enabled to make sure no other site is using the ServerName or ServerAlias directive that conflicts with your sites.

Bobby Iliev
Site Moderator
Site Moderator badge
August 16, 2023

Hi there!

It looks like you’ve set up the VirtualHost for the second site correctly for port 80, but if you’re accessing the site via HTTPS, you’ll also need to create a VirtualHost block for port 443, similar to what you’ve done for the original site.

What you could do is use Let’s Encrypt to get a free SSL and also together with certbot it will automatically configure your Apache virtual host for port 443:

https://www.digitalocean.com/community/tutorials/how-to-secure-apache-with-let-s-encrypt-on-ubuntu-20-04

Hope that this helps!

Best,

Bobby

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel