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.

To start with, we wanted a request to *.example.com to be handled by a corresponding /var/www/*/ directory, and we have got this working by using mod_vhost_alias with VirtualDocumentRoot, as follows:

<VirtualHost *:80>
    ServerAlias *.example.com
    UseCanonicalName Off
    VirtualDocumentRoot /var/www/%1/
</VirtualHost>

Now we want to expand on this shared configuration by applying the same RewriteRules to all subdomains / directories - from within the vhost configuration's <Directory> block - but we do not know how to reference the subdomain / directory.

We're basically trying to do something like this (note the use of %1, which doesn't work in this case):

<VirtualHost *:80>
    ServerAlias *.example.com
    UseCanonicalName Off
    VirtualDocumentRoot /var/www/%1/

    <Directory /var/www/%1>
            RewriteEngine on
            RewriteRule     ^about/?$       index.php?view=about
            RewriteRule     ^settings/?$    index.php?view=settings
            RewriteRule     ^support/?$     index.php?view=support
    </Directory>
</VirtualHost>

Is something like this possible?

Thanks.

share|improve this question
add comment

2 Answers 2

up vote 1 down vote accepted

Ok we've managed to get this to work. All that was needed was a minor tweak of the <Directory> path (namely using the * wildcard instead of %1), removing the ^ characters in the RewriteRules' patterns, and adding a leading / in the substitution strings:

<VirtualHost *:80>
    ServerAlias *.example.com
    UseCanonicalName Off
    VirtualDocumentRoot /var/www/%1/

    <Directory /var/www/*>
        RewriteEngine on
        RewriteRule     about/?$       /index.php?view=about
        RewriteRule     settings/?$    /index.php?view=settings
        RewriteRule     support/?$     /index.php?view=support
    </Directory>
</VirtualHost>

Now for the sake of complying with ServerFault's policy (as well as Google's indexing), I'm marking this question as answered, but I must emphasise that we couldn't have done this without the patient help from thumbs in #httpd on irc.freenode.net

share|improve this answer
add comment

Don't bother with <Directory> in this case.

<VirtualHost *:80>
    ServerAlias *.example.com
    UseCanonicalName Off
    VirtualDocumentRoot /var/www/%1/

    RewriteEngine on
    RewriteRule     ^/about/?$       /index.php?view=about
    RewriteRule     ^/settings/?$    /index.php?view=settings
    RewriteRule     ^/support/?$     /index.php?view=support
</VirtualHost>

If you've got something other than a Rewrite that needs to be done, for instance, access controls, add a <Location> block instead of a <Directory> block:

<Location />
    Order deny,allow
    Deny from all
    Allow from 10.0.0.0/8
</Location>
share|improve this answer
    
Thanks Shane. I tried this but I get a 404 error. I checked my access.log and it seems that the URLs are not being re-written correctly. For example, http://foo.example.com/about/ is being re-written to http://foo.example.com/about/index.php?view=about instead of http://foo.example.com/index.php?view=about - and I also checked the error.log and it says "File does not exist: /htdocs". Any ideas? –  Kosta Kontos Sep 19 '11 at 21:48
add comment

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.