apache map single subdomain to folder
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
Related videos on Youtube
Bill VB
Updated on July 09, 2022Comments
-
Bill VB almost 2 years
In my
/var/www
I have a number of sites (goodsite, badsite, uglysite). Right now they are accessed bymydomain.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 almost 12 yearsYou 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 almost 12 yearsDo I need to explicitly include it anywhere?
-
arcyqwerty almost 12 yearsDont forget to add the site to the apache.conf file and restart apache (
sudo service apache2 restart
) once you've finished your configuration -
arcyqwerty almost 12 yearsDepending 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 almost 12 yearsUnfortunately, 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 almost 12 yearsDid you make sure that the DocumentRoot is pointing to the appropriate directory in both configuration virtualhost files? Did you restart apache?
-
Bill VB almost 12 yearsDocumentRoot 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 almost 12 yearsHow 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 almost 12 yearsYou may need to use the
ServerAlias
directive -
Bill VB almost 12 yearsYes I have a virtualhost on *:81... How should I use ServerAlias (thanks for the help thus far)
-
arcyqwerty almost 12 yearsAlso, if you need to debug your configuration / see what apache is doing, try running the apache executable
/usr/local/apache2/bin/httpd -S
orapache2ctl -S