Thursday, October 30, 2014

apache 2.2 - Subdomains work fine, but redirect to main when using CNAME



I have a server with multiple websites in subfolders that I want to give their own domains. I'm got two subdomains setup using VirtualHost as such:




DocumentRoot "/var/www/ex1"
ServerName ex1.domain.com




DocumentRoot "/var/www/ex2"
ServerName ex2.domain.com



They are setup as A records in my DNS, and they work fine when accessing ex1.domain.com and ex2.domain.com, and the main domain www.domain.com works as well.



However, when I setup their main domains, www.example1.com as CNAME record redirecting to ex1.domain.com, visting www.example1.com shows me www.domain.com and not ex1.domain.com as it should.



What am I doing wrong?



Answer



ServerAlias directive should do the trick:




DocumentRoot "/var/www/ex1"
ServerName ex1.domain.com
ServerAlias www.example1.com




DocumentRoot "/var/www/ex2"
ServerName ex2.domain.com
ServerAlias www.example2.com


No comments:

Post a Comment

linux - How to SSH to ec2 instance in VPC private subnet via NAT server

I have created a VPC in aws with a public subnet and a private subnet. The private subnet does not have direct access to external network. S...