Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I've been searching for a way to automatically redirect domains to a specific folder, and fond a good answer here on serverfault: Apache2 VirtualHost auto subdomain. (The accepted answer)

So far everything works good, however now I need to add an exception to this. The result I want is this:

http://localhost/        -->   E:/websites/
http://specialDomain2/   -->   E:/websites/      -->   E:/websites/ -->   E:/websites/

I get the expceted result for the two last domains, but the localhost doesn't work.

I copied the script from the question aboved, and tried to add something like

<VirtualHost *:80>
    RewriteEngine On
    RewriteMap lowercase int:tolower
    # if already rewitten and we have the right path, stop right here
    RewriteRule ^(E:/websites/[^/]+/.*)$ $1 [L]
    RewriteRule ^localhost/(.*)$ E:/websites/$1 [L] # <-- Added this row
    RewriteRule ^(.+) ${lowercase:%{SERVER_NAME}}$1 [C]
    RewriteRule ^(www\.)?([^/]+)/(.*)$ E:/websites/$2/$3 [L,E=VHOST_ROOT:E:/websites/$2/]

I thought this would make sense, since I would translate this to

if URL = localhost/*
   Do nothing (because of the [L] flag), and use the default document root specified earlier

What's wrong with this? Thanks for any help!

share|improve this question
up vote 1 down vote accepted

Actually Apache is able to that already. If you have activated name based virtual hosting there is one default host which is responsible for non matching host names.

A host name matches if the ServerName or ServerAlias is matching. If none of the configured virtual hosts provide a match for a request the default host will be used. This is usually the first virtual host in your configuration.

NameVirtualHost *:80

# default virtual host
<VirtualHost *:80>
  ServerName localhost
  DocumentRoot /www/localhost

<VirtualHost *:80>
  DocumentRoot /www/

<VirtualHost *:80>
  DocumentRoot /www/
share|improve this answer
That's actually how I had it set up before I wanted to organize things a bit more. The problem with this is that I have to add an entry to my config for each new site. As the amount of websites builds up, so does the config file. I'd prefer a fully automated process. – Henrik Karlsson Apr 16 '12 at 6:00
As the config files are simple text you can export your config from a database and generate config files automatically. This is quite common. – Jens Bradler Apr 16 '12 at 6:15
That was actually a quite good idea, but you still need a server restart for each new site then, right? – Henrik Karlsson Apr 16 '12 at 10:05
Apache makes a difference between restart (HUP signal) and reload (USR1 signal). The reload command can be executed by "apache2ctl graceful" or "service apache reload" (depends on your system). Such a graceful reload means, that children will wait until requests are answered before they are restarted. – Jens Bradler Apr 16 '12 at 13:57
Ah, thanks for that information. I guess I have a great deal to learn about apache yet.. I'll choose your answer as accepted even tho it wasn't exactly what I was serching for. Thanks! – Henrik Karlsson Apr 16 '12 at 15:12

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.