1

I currently have one main domain: mydomain.com

This is working fine.

What im trying to do is now adding a subdomain through my mydomain.conf file, so that I can have:

sub.mydomain.com

I've tried with the following - however the result of this is that im serving the content from mydomain.com at sub.mydomain.com:

# MY MAIN DOMAIN
<VirtualHost *:80>
ServerName mydomain.com
ServerAlias *.mydomain.com
DocumentRoot /var/www/mydomain.com/html
</VirtualHost>

# DEFINING MY SUBDOMAIN
<VirtualHost *:80>
ServerName sub.mydomain.com
ServerAlias sub.mydomain.com
DocumentRoot /var/www/sub/html/
</VirtualHost> 

I've naturally created the DocumentRoot for the subdomain at: DocumentRoot /var/www/sub/html/

At my DNS-supplier, i've created an A-record for both: sub.mydomain.com www.sub.mydomain.com - and pointed it to my server IP. (the same IP that mydomain.com is pointing to)

Im running Centos 6.2.

Any ideas what is wrong here?

3

The problem is that your first VirtualHost has the line

ServerAlias *.mydomain.com

This matches all subdomains. Since VirtualHosts are parsed in the order they appear in the config file, Apache will find this VH and never go on to look at the next one.

In other words, you will need to move the two domains around so that the VirtualHost entry for the subdomains apears above the one for the main domain.

  • By the way, if you're going to be using a lot of subdomains, you might be interested in using httpd.apache.org/docs/2.2/mod/mod_vhost_alias.html to simplify the configuration. – Jenny D Jan 9 '13 at 15:35
  • Hi Jenny - thanks a lot for the guidance. Your re-arranging trick did it for me. Ill surely also look into the url you posted. Appreciate the help! Thanks again – user1231561 Jan 9 '13 at 19:06
2
Your virtual host entry should be like this. Hope it will help you.

<VirtualHost mydomain.dev:80>
     ServerName www.yourdoamin.com
     ServerAlias yourdomain.com *.yourdomain.com
     DocumentRoot /var/www/path_of_folder
</VirtualHost>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.