apache 2 – how to access "mydomain.com" OR "www.mydomain.com"

Greg asked:

I’ve recently set up a domain name through godaddy.com. I edited my A record to point to my servers IP address. I then set up a sites-available file and symlinked that to the site-enabled directory. When I go to the url “mydomain.com” I get the index.html file in /var/www/index.html which just says “No Direct Access.” When I go to the url “www.mydomain.com” I get my app properly. My sites-available file for the domain is fairly basic, looks like:

<VirtualHost *:80>
    ServerName www.mydomain.com
    DocumentRoot /path/to/my/app
    <Directory />
        ....
    </Directory>
    <Directory /path/to/my/app/>
        Option FollowSymLinks MultiViews
        AllowOverride All
        Order allow,deny
        allow from all
    </Directory>

When I try this sites-available file WITHOUT the www in the server name, I can not get to the app either way. When I add the “www” to the server name (as it is above) I get the app, but can not then go to just “mydomain.com” or I get the “No Direct Access” error. Anyone know how I can use either “www.mydomain.com” or just “mydomain.com” to access the site?

My answer:


Add a ServerAlias.

ServerName www.mydomain.com
ServerAlias mydomain.com

View the full question and answer on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.