Hey Marco,
I thought I knew what you was trying to do, but now I think I don't :(
The rewrite rule that we started with does exactly that, i.e. it
rewrites the clients request domain1.com to www.domain1.com. I thought
this was what you were trying to do originally.
Anyway, now I think you are just trying to create site which has name
based virtual hosts, which respond to domainx.com and www.domainx.com so
here goes...
First you need your main site, let's call it www.domain.com and the
ip-address for your site is xxx.xxx.xxx.xxx
You create a file 000-domain in your /etc/apache2/sites-enabled (you
actually create it in /etc/apache2/sites-available directory and symlink
it in the /etc/apache2/sites-enabled). The file is something like this:
NameVirtualHost xxx.xxx.xxx.xxx
<VirtualHost xxx.xxx.xxx.xxx>
ServerAdmin webmaster@domain.com
ServerName www.domain.com
DocumentRoot /var/www/
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory>
ErrorLog /var/log/apache2/error.log
LogLevel warn
CustomLog /var/log/apache2/access.log combined
ServerSignature On
</VirtualHost>
Then you create a file 001-domain1 for your first virtual host
<VirtualHost xxx.xxx.xxx.xxx>
ServerAdmin webmaster@domain1.com
ServerName www.domain1.com
ServerAlias domain1.com
DocumentRoot /var/www/domain1
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /var/www/domain1>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order deny,allow
Allow from all
</Directory>
ErrorLog /var/log/apache2/domain1-error.log
LogLevel warn
CustomLog /var/log/apache2/domain1-access.log combined
ServerSignature On
</VirtualHost>
Then you create a file 002-domain2 for your second virtual host
<VirtualHost xxx.xxx.xxx.xxx>
ServerAdmin webmaster@domain2.com
ServerName www.domain2.com
ServerAlias domain2.com
DocumentRoot /var/www/domain2
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /var/www/domain2>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order deny,allow
Allow from all
</Directory>
ErrorLog /var/log/apache2/domain2-error.log
LogLevel warn
CustomLog /var/log/apache2/domain2-access.log combined
ServerSignature On
</VirtualHost>
And restart your apache.
Now you have three severs, first one is the default if none of the other
ones have matching server name for the request. You have your main
server files in /var/www directory, the first virtual host files in
/var/www/domain1 and the second virtual host files in /var/www/domain2.
The virtual servers will respond to domainx.com and www.domainx.com and
only for those two variants and nothing gets rewritten.
You also have the per server log files in your /var/log/apache2/.
Let me know if this is what you are trying to achieve.
Simo