0

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/
http://normal1.com/      -->   E:/websites/normal1.com/
http://normalDomain.com/ -->   E:/websites/normalDomain.com/

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/]
</VirtualHost>

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
else
   continue

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

1 Answer 1

1

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>  

<VirtualHost *:80>
  ServerName normal1.com
  ServerAlias www.normal1.com
  DocumentRoot /www/normal1.com
</VirtualHost>

<VirtualHost *:80>
  ServerName normalDomain.com
  ServerAlias www.normalDomain.com
  DocumentRoot /www/normalDomain.com
</VirtualHost>
5
  • 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. Apr 16, 2012 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. Apr 16, 2012 at 6:15
  • That was actually a quite good idea, but you still need a server restart for each new site then, right? Apr 16, 2012 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. Apr 16, 2012 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! Apr 16, 2012 at 15:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .