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>
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!
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.
Enter your email to get $200 in credit for your first 60 days with DigitalOcean.
New accounts only. By submitting your email you agree to our Privacy Policy.
Heya,
It seems you have a few issues that might be contributing to the problem:
Overlap of
<VirtualHost *:80>
Blocks: Youroriginalsite.com
configurations have multiple overlapping blocks that can confuse Apache on how to handle requests.RewriteRule and RewriteCond:
originalsite.com
will redirect all traffic towww.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.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.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:
Consolidate VirtualHosts for originalsite.com:
Combine the configurations for
originalsite.com
andwww.originalsite.com
under port 80:Setup the Second Site:
Make sure that you have enabled the vhost for
second.com
:Add HTTPS Configuration for Second Site:
Ensure that
second.com
also has an HTTPS (port 443)<VirtualHost>
block similar tooriginalsite.com
.Ensure No Other Conflicting Configurations:
Check other configurations under
sites-available
andsites-enabled
to make sure no other site is using theServerName
orServerAlias
directive that conflicts with your sites.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:Hope that this helps!
Best,
Bobby