Dynamic Virtual Hosts In Apache with www and non-www subdomains

haukish asked:

I don’t know apache very well and I’ve got a problem with configure mod_vhost_alias

This is my httpd.conf file:

UseCanonicalName Off

LogFormat "%V %h %l %u %t "%r" %s %b" vcommon

<Directory /var/www/sites/>
Options FollowSymLinks
AllowOverride All
</Directory>

<VirtualHost *:80>
    CustomLog logs/access_log.sites vcommon
    ServerAlias *.domain.com
    UseCanonicalName Off
    VirtualDocumentRoot /var/www/sites/%1/
</VirtualHost>

Subdomains work fine without www. but I need to make them work with www too.

Here’s an example:

something.domain.com - site is loading
www.something.domain.com - Not Found

What should I do?

My answer:


something.domain.com and www.something.domain.com are obviously different hostnames. And Apache will substitute this for %1 when looking for the files to serve. So the problem is that /var/www/sites/www.something.domain.com doesn’t exist.

One way to fix it (assuming these two sites are supposed to serve the same data) is by a simple symbolic link:

ln -s something.domain.com /var/www/sites/www.something.domain.com

(I also missed that you need %0 instead of %1, as in Shane Madden’s answer. So do that too.)


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.