Can you setup vistual hosts using only a .htaccess file?

Hi There,

Our host doesn’t allow access to the httpd.conf file so I need to setup a virtual host using only the .htaccess file.

I guess I need to accomplish something like this:

NameVirtualHost *:80

<VirtualHost *:80>
ServerName www.domain-one.com
ServerAlias domain-one.com
DocumentRoot /usr/local/www/vhosts/domain-one.com/httpdocs/
</VirtualHost>

<VirtualHost *:80>
ServerName www.domain-two.com
ServerAlias domain-two.com
DocumentRoot /usr/local/www/vhosts/domain-one.com/httpdocs/domain-two/
</VirtualHost>

But in a .htaccess file - is this possible? I realise I can’t use <VirtualHost> in the .htaccess file but is there a way to accomplish a similar thing using .htaccess? I guess I just need to direct incoming domain names to their correct hosting folders - which are:

/
/site-two
/site-three
/site-four

Eventually there will be 4 sites running off this hosting account and I’ve been playing around with this (which doesn’t seem to work):

Options +FollowSymlinks
RewriteEngine On
RewriteCond %{HTTP_HOST} domain-two.com
RewriteCond %{REQUEST_URI} !^site-two
RewriteRule ^(.*)$ site-two/$1 [L]

Any help or suggestions would be much appreciated - my Apache knowledge is very limited so go easy!

Cheers

Ben

This seems to work but it directs all urls to the home page (the site-two hosting folder):

RewriteCond %{HTTP_HOST} domain-two.com
RewriteCond %{REQUEST_URI} !^/site-two
RewriteRule ^(.*)$ site-two/$1 [L]

So the url “domain-two.com/about/” also goes to the home folder instead of the about page.

Ben,

Actually, your knowledge isn’t bad. It’s the simple fact that Apache cannot create a new VirtualHost and especially not from an .htaccess file!

If your host is offering cPanel (or another, similar control panel), then you can use that to create new VirtualHosts AND specify their document root directories.

Regards,

DK

Thanks David, the host uses Plesk and I can’t create virtual hosts on that account via the Plesk control panel - hence trying to find some way of doing this via .htaccess

So to recap, are you saying this isn’t possible via .htaccess? Just trying to figure this all out - have to go live on Friday!

cmscss,

This is a DNS question, NOT specific to Apache (and it cannot be resolved via .htaccess). If Plesk can’t do this (I’m sure it can - UNLESS your host has disabled this - look for subdomains), contact your host directly.

Regards,

DK