Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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?

share|improve this question

2 Answers 2

up vote 0 down vote accepted

VirtualDocumentRoot /var/www/sites/%1/ is looking at the first part of the name - so for something.domain.com it's using something, and for www.something.domain.com it's using www, which won't work very well.

A couple different options:

  1. Get it to use something as the directory it's looking for in both cases; instead of the first part of the name, look at the third-to-last:

    VirtualDocumentRoot /var/www/sites/%-3/
    
  2. Switch over to using the full name, and then create a symbolic link (see Michael's answer):

    VirtualDocumentRoot /var/www/sites/%0/
    
share|improve this answer
    
I'm not sure where should I create that link (you can write for future) but %-3 did the trick. Thanks man. –  haukish Dec 15 '12 at 20:39
    
@haukish You'd do it in /var/www/sites, so inside there you would have something.domain.com (the real directory) and www.something.domain.com (the symbolic link). –  Shane Madden Dec 15 '12 at 20:40
    
I think I know what you mean, but in this case I'll have to do it every time I create new directory for new subdomain. Am I right? %-3 seems to work right, but I'll keep it in my mind, thx again. –  haukish Dec 15 '12 at 20:45
    
@haukish Yup, you'd need a new link for every sub-domain. The trade-off is that %-3 will break if you add an extra level to the subdomains - so www.one.some.domain.com and two.some.domain.com will both use some - the extra layers would be ignored. –  Shane Madden Dec 15 '12 at 20:48
    
I think I wouldn't need next level on this domain but thanks for mention –  haukish Dec 15 '12 at 20:53

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.)

share|improve this answer
    
I believe it looks for "www.something". Can I add something like ln -s *.domain.com /var/www/sites/www.*.domain.com I need it for more than one domain. And where should I put it? –  haukish Dec 15 '12 at 19:56
    
If it did, you would have gotten a 404 for something. and your site for www.something. –  Michael Hampton Dec 15 '12 at 20:18
    
I created www.something.domain.com and www.something directories in /var/www/sites but there is still 404 :/ –  haukish Dec 15 '12 at 20:24
    
thanks you too. –  haukish Dec 15 '12 at 20:40

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.