Report this

What is the reason for this report?

Multiple vhosts all redirecting to one

Posted on August 11, 2023
tim

By tim

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>

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Start building today

From GPU-powered inference and Kubernetes to managed databases and storage, get everything you need to build, scale, and deploy intelligent applications.