apache map single subdomain to folder

Bill VB picture Bill VB · Jul 17, 2012 · Viewed 19.3k times · Source

In my /var/www I have a number of sites (goodsite, badsite, uglysite). Right now they are accessed by mydomain.com/goodsite, etc..

What I want is for one site in particuar, uglysite, to be accessed by uglysite.mydomain.com - the others remain as they are.

I have tried all sorts of ways of fiddling with the.htaccess (in /var/www). Note I have mod-rewrite enabled and mod vhost-alias enabled.

RewriteEngine on
RewriteCond %{HTTP_HOST} ^uglysite\.mydomain\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/uglysite/
RewriteRule ^(.*)$ /uglysite/$1 [L]

What ends up happening is that both mydomain.com and uglysite.mydomain.com always map to the same thing (i.e., the index at /var/www). I tried adding in a new virtual host, and was surprised to find that uglysite.mydomain.com mapped correctly, but then mydomain.com also mapped directly to uglysite as well.

<Virtualhost uglysite.mydomain.com:80>
       ServerName uglysite.mydomain.com
       ServerAdmin www@localhost
       DocumentRoot "/var/www/"
       AccessFileName .htaccess
       <Directory "/var/www/uglysite">
               Order allow,deny
               Allow from All
               AllowOverride All
       </Directory>
</VirtualHost>

The above was added to my sites-enabled/000-default file. This got uglysite.mydomain.com to work properly, but then mydomain.com mapped to the same thing!

Is there a straightforward way to do what I'm intending to do?? Thanks in advance.

Answer

arcyqwerty picture arcyqwerty · Jul 17, 2012

You should be making uglysite into a second file instead of modifying 000-default

So, take a copy of the 000-default file, change the subdomain as you have done up there and modify the directory to /path/to/site

000-default:

<Virtualhost *:80>
       ServerName mydomain.com
       ServerAdmin www@localhost
       ServerAlias mydomain.com
       DocumentRoot "/var/www/goodsite"
       AccessFileName .htaccess
       <Directory "/var/www/goodiste">
               Order allow,deny
               Allow from All
               AllowOverride All
       </Directory>
</VirtualHost>

uglysite:

<Virtualhost *:80>
       ServerName uglysite.mydomain.com
       ServerAlias uglysite.mydomain.com
       ServerAdmin www@localhost
       DocumentRoot "/var/www/uglysite"
       AccessFileName .htaccess
       <Directory "/var/www/uglysite">
               Order allow,deny
               Allow from All
               AllowOverride All
       </Directory>
</VirtualHost>

Also note that in the above samples, I have modified the DocumentRoot to point to the directory that you want file served from

EDIT: virtualhosts set to *:80 since your sites point to your own ip anyway