Apache Virtual Host Configuration: Fixing “It works!” Page When Accessing Domain Without www Prefix


22 views

When your domain shows the default "It works!" page without the www prefix but works normally with www, this indicates a misconfigured virtual host in Apache. The server is falling back to the default configuration because it can't find a matching ServerName or ServerAlias for the naked domain.

First, let's examine your existing configuration. Look for files in:

/etc/apache2/sites-available/
or
/etc/httpd/conf.d/

A typical working configuration for www should look like:

<VirtualHost *:80>
    ServerName www.domain.com
    DocumentRoot /var/www/html/domain
    # Other directives...
</VirtualHost>

You need to either:

  1. Add ServerAlias for the naked domain in your existing config
  2. Create a separate VirtualHost for the naked domain

Here's the recommended approach:

<VirtualHost *:80>
    ServerName domain.com
    ServerAlias www.domain.com
    DocumentRoot /var/www/html/domain
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

1. Edit your configuration file:

sudo nano /etc/apache2/sites-available/domain.conf

2. Add the configuration shown above

3. Enable the site and reload Apache:

sudo a2ensite domain.conf
sudo systemctl reload apache2

Verify your setup with:

apache2ctl -S

This will show all configured virtual hosts and help identify any conflicts.

Many developers prefer to redirect the naked domain to the www version for consistency:

<VirtualHost *:80>
    ServerName domain.com
    Redirect permanent / http://www.domain.com/
</VirtualHost>

<VirtualHost *:80>
    ServerName www.domain.com
    DocumentRoot /var/www/html/domain
    # Other directives...
</VirtualHost>

If changes don't take effect:

  • Clear your browser cache
  • Check for syntax errors: apache2ctl configtest
  • Verify DNS records propagate completely

Many developers encounter this classic Apache configuration issue where accessing domain.com shows the default "It works!" page while www.domain.com works correctly. This happens because:

  • Apache's default virtual host configuration only responds to explicit hostnames
  • The non-www version isn't properly defined in your VirtualHost directives
  • Apache falls back to its default site configuration

Here's what a typical problematic configuration might look like:



    ServerName www.domain.com
    DocumentRoot /var/www/html/domain
    # Missing ServerAlias for domain.com

The most robust fix involves updating your VirtualHost configuration:



    ServerName domain.com
    ServerAlias www.domain.com
    DocumentRoot /var/www/html/domain
    
    # Optional but recommended
    ErrorLog ${APACHE_LOG_DIR}/domain_error.log
    CustomLog ${APACHE_LOG_DIR}/domain_access.log combined

For those needing more flexibility, consider these options:

1. Redirect Solution



    ServerName domain.com
    Redirect permanent / http://www.domain.com/



    ServerName www.domain.com
    DocumentRoot /var/www/html/domain

2. Wildcard Configuration



    ServerName domain.com
    ServerAlias *.domain.com
    DocumentRoot /var/www/html/domain

After making changes:

  1. Run sudo apache2ctl configtest to check syntax
  2. Restart Apache: sudo systemctl restart apache2
  3. Clear browser cache before testing
  • Check /etc/apache2/sites-enabled/ for conflicting configurations
  • Ensure no duplicate VirtualHost entries exist
  • Verify DNS records point to the correct server IP