apache map single subdomain to folder

21,466

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

Share:
21,466

Related videos on Youtube

Bill VB
Author by

Bill VB

Updated on July 09, 2022

Comments

  • Bill VB
    Bill VB almost 2 years

    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.

  • arcyqwerty
    arcyqwerty almost 12 years
    You should be making this file in /etc/apache2/sites-available btw and symlinking (ln -s sites-available/uglysite sites-enabled/uglysite) it into sites-enabled
  • Bill VB
    Bill VB almost 12 years
    Do I need to explicitly include it anywhere?
  • arcyqwerty
    arcyqwerty almost 12 years
    Dont forget to add the site to the apache.conf file and restart apache (sudo service apache2 restart) once you've finished your configuration
  • arcyqwerty
    arcyqwerty almost 12 years
    Depending on how you have your apache server set up, you may or may not need to include it in apache.conf. Check to see if there is already an Include sites-enabled/ line near the end
  • Bill VB
    Bill VB almost 12 years
    Unfortunately, now I'm at my original problem: now uglysite.mydomain.com works perfectly just as I wanted, but now mydomain.com also maps to uglysite.mydomain.com!
  • arcyqwerty
    arcyqwerty almost 12 years
    Did you make sure that the DocumentRoot is pointing to the appropriate directory in both configuration virtualhost files? Did you restart apache?
  • Bill VB
    Bill VB almost 12 years
    DocumentRoot is pointing to the correct folders. however, if I take the VirtualHost for *:80 and change it to mydomain.com:80 and reset apache - I get a slew of errors: "overlaps with VirtualHost ... the first has precedence, perhaps you need a NameVirtualHost directive"
  • arcyqwerty
    arcyqwerty almost 12 years
    How many files do you have right now? Do you have something other than the two files I put in my answer? (is the *:80 in your default-000 right now?)
  • arcyqwerty
    arcyqwerty almost 12 years
    You may need to use the ServerAlias directive
  • Bill VB
    Bill VB almost 12 years
    Yes I have a virtualhost on *:81... How should I use ServerAlias (thanks for the help thus far)
  • arcyqwerty
    arcyqwerty almost 12 years
    Also, if you need to debug your configuration / see what apache is doing, try running the apache executable /usr/local/apache2/bin/httpd -S or apache2ctl -S