0

I have a problem whereby my VirtualHost is overriding another VirtualHost on the same domain.

httpd.conf

IncludeOptional conf.d/*.conf

example.conf

<VirtualHost *:80>
        DocumentRoot /var/www/html/example/
        ServerName www.example.com
        ServerAlias www.example.com
    Redirect permanent / https://www.example.com/
</VirtualHost>
<VirtualHost *:443>
    DocumentRoot /var/www/html/example/
    SSLEngine on
    SSLCertificateFile /etc/httpd/ssl/example/example.crt
    SSLCertificateKeyFile /etc/httpd/ssl/example/example.key
    SSLCertificateChainFile /etc/httpd/ssl/example/chain.cer
    ServerName www.example.com
    ServerAlias www.example.com
</VirtualHost>

get.example.conf

<VirtualHost *:80>
        DocumentRoot /var/www/html/get.example/
        ServerName get.example.com
        ServerAlias get.example.com
</VirtualHost>

When I created get.example.conf and when I accessed example.com, it is showing the content of get.example.com

Does anyone knows what is going wrong with my configuration?

Shawn Ang
  • 538
  • 7
  • 21
  • Because, you don't have any configuration for example.com, only for www.example.com. Try adding ServerName example.com and ServerAlias www.example.com. It's all. – Jose Ayram Aug 01 '19 at 19:59

3 Answers3

0

Found out that I need to include the non-www for the VirtualHost as well. It works if I entered www.example.com but not example.com.

<VirtualHost *:80>
    ServerName example.com
    Redirect permanent / http://www.example.com/
</VirtualHost>
Shawn Ang
  • 538
  • 7
  • 21
0

You just have to set up de ServerAlias. In your example, it should be enough to do as follow:

<VirtualHost *:80>
        DocumentRoot /var/www/html/example/
        ServerName example.com
        ServerAlias www.example.com
    Redirect permanent / https://www.example.com/
</VirtualHost>

Note that ServerName doesn't has www., so that it's gonna match http://example.com, while with the ServerAlias it's gonna match http://www.example.com

GiuServ
  • 1,215
  • 1
  • 13
  • 33
-1

Try to change the order of ServerName and ServerAlias to your get.example.conf:

<VirtualHost *:80>
        ServerName get.example.com
        ServerAlias get.example.com
        DocumentRoot /var/www/html/get.example/
</VirtualHost>
Jonathan Nuñez
  • 432
  • 6
  • 19