Skip to content

How to Fix Apache Virtual Host Conflict Error

DodaTech Updated 2026-06-24 2 min read

In this tutorial, you'll learn about How to Fix Apache Virtual Host Conflict Error. We cover key concepts, practical examples, and best practices to help you understand and apply this topic effectively.

Apache serves the wrong website for a domain or shows the default site instead of the configured virtual host — the virtual host ordering is incorrect or multiple virtual hosts match the same ServerName.

The Problem

$ curl -H "Host: example.com" http://localhost
# Returns content from a different site or the default Apache page

Step-by-Step Fix

Step 1: Check virtual host ordering

# /etc/apache2/sites-available/000-default.conf
# The first virtual host for a port becomes the default
# Place your main site first

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

Step 2: Use explicit ServerName and ServerAlias

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

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

Step 3: Disable conflicting sites

sudo a2dissite 000-default.conf
sudo a2ensite example.com.conf
sudo systemctl reload apache2

Step 4: Check enabled sites

ls -la /etc/apache2/sites-enabled/

Expected: Only the sites you want enabled.

Step 5: Test name-based virtual hosting

apachectl -S

Expected:

VirtualHost configuration:
*:80    example.com (/etc/apache2/sites-enabled/example.com.conf:1)
*:80    other.com (/etc/apache2/sites-enabled/other.com.conf:5)

Prevention Tips

  • Always set explicit ServerName on every virtual host
  • Disable the default site (000-default.conf) in production
  • Use apachectl -S to verify virtual host configuration
  • Keep one virtual host per file for clarity

Common Mistakes with vhost conflict

  1. Non-exhaustive pattern matches that compile with warnings then crash at runtime
  2. Misunderstanding that String is [Char] with poor performance for large text operations
  3. Using foldl instead of foldl' causing stack overflow on large lists

These mistakes appear frequently in real-world APACHE code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.

Practice Exercise

Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.

This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.

FAQ

### Why does Apache serve the wrong site for a domain?

When no virtual host matches the requested ServerName, Apache uses the first-defined virtual host for that port. Disable the default site and ensure each domain has a matching virtual host with the correct ServerName.

What is the difference between ServerName and ServerAlias?

ServerName is the primary domain for the virtual host. ServerAlias specifies additional domain names that should also match this virtual host, such as www.example.com or example.net.

How do I set up HTTP to HTTPS redirect with virtual hosts?

<VirtualHost *:80>
    ServerName example.com
    Redirect permanent / https://example.com/
</VirtualHost>

<VirtualHost *:443> ServerName example.com DocumentRoot /var/www/example SSLEngine on SSLCertificateFile /etc/ssl/certs/example.crt </VirtualHost>

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro