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

I'm using the configuration below to use vhost_alias_module to more easily manage my hosted setup. I want sites to be structured like the following /sites/domain.com/w/w/w/ which with the configuration below works fine. However, if I attempt to access domain.com instead of www.domain.com this falls apart. How do I fix this?

Update: I want request for domain.com to automatically be directed to www.domain.com. It would be fine if this required a 403 redirect (I think that would be preferred from a SEO perspective) .

LoadModule vhost_alias_module /usr/lib/apache2/modules/mod_vhost_alias.so
<VirtualHost *:80>
        ServerAdmin admin@gmail.com

        DocumentRoot /sites/
        <Directory />
                Options FollowSymLinks
                AllowOverride None
        <Directory /sites/>;
                Options Indexes FollowSymLinks MultiViews ExecCGI
                AllowOverride All
                Order allow,deny
                allow from all
    UseCanonicalName Off
    VirtualDocumentRoot /sites/%2+/%1.1/%1.2/%1.3+

share|improve this question
How do you want it to behave? /sites/domain.com/? /sites/domain.com/d/o/m? Or requests without a subdomain imply www? –  Shane Madden Dec 15 '11 at 20:08
@ShaneMadden good point I did forget to write that. :-) Imply www if it's missing. –  Scott Warren Dec 15 '11 at 20:15

1 Answer 1

up vote 1 down vote accepted

Well, the requirement to imply www comes close to pushing you out of being able to use mod_vhost_alias, and into mod_rewrite territory.. but there's a couple options that I can think of:

  1. Just redirect to www.

    RewriteEngine on
    # Match anything with a two-part name
    # (will not work for 3-part base domains that would need to redirect to www, be careful)
    RewriteCond %{HTTP_HOST} ^([^\.]+\.[^\.]+)$
    # Redirect
    RewriteRule ^/(.*)$ http://www.%{HTTP_HOST}/$1 [R=301]
  2. Let those requests work without a redirect, and use symlink trickery to stick them to the www location.

    With your VirtualDocumentRoot, requests to domain.com go to /sites/com/d/o/m - let's change that:

    VirtualDocumentRoot /sites/%-2+/%-3.1/%-3.2/%-3.3+

    This will make a request for domain.com go to /sites/domain.com/_/_/_ - make this location a symlink to /sites/domain.com/w/w/w.

share|improve this answer
Thanks for the answer, 2 seems like a lot of work if I have more than a few domains but would be a quick fix. Where would I put the mod_rewrite section in #1? Would that go just before my VirtualDocumentRoot directive? –  Scott Warren Dec 16 '11 at 19:12
@ScottWarren Anywhere in that <VirtualHost> block. And keep in mind that it's pretty rudimentary, it's just looking for two-part names and tacking a www on the front; if you have someapplication.clientdomain.com that needs to direct to www.someapplication.clientdomain.com, it won't work. –  Shane Madden Dec 16 '11 at 19:19
That will work just fine for what I'm planning. –  Scott Warren Dec 16 '11 at 20:55

Your Answer


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.