0

I have two versions of applications running on an apache server.Now I have to point one to the main domain viz www.example.com and the other to a subdomain mysite.example.com.

The challenge I am facing is if I type in only example.com it goes to mysite.example.com instead of www.example.com.

I do not have anything configured in the .htaccess files as far as redirection is concerned. Also DNS entries for both www and example.com are pointing to the same ip

My conf files are as below:

This is for the subdomain

    ServerName mysite.example.com
    #ServerAlias mysite.example.com      // not sure if i need alias
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html/site1

    <Directory /var/www/html/site1>
     Options Indexes FollowSymLinks MultiViews
     AllowOverride All
     Order allow,deny
     allow from all
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

  </VirtualHost>

This one for the main domain

 <VirtualHost *:80>

    ServerName www.example.com
    ServerAlias example.com      
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html/site2

    <Directory /var/www/html/site2>
     Options Indexes FollowSymLinks MultiViews
     AllowOverride All
     Order allow,deny
     allow from all
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

  </VirtualHost>

I want to serve one version for the subdomain and the other on the main domain,with all URL possibilities covered.

Can anybody point where I am going wrong on this?

0

I'm no expert in this, but I myself want to run a local hosted server with online capabilities too, I did ran it a few months ago, but now I'm getting back to it, and kinda rusty to be honest, so I'm re-learning everything back with the mess to work with VHosts and setup local servers to later be online and ... it's a mess lol... But what I see and I quote:

The challenge I am facing is if I type in only example.com it goes to mysite.example.com instead of www.example.com.

However you mentioned the config below is for your subdomain... In which case it should be inverted... you see you're pointing your main domain to your: "/var/www/mysite" which should be used for your subdomain... mysite.example.com instead. So I'm guessing you're confusing the entries for the directories.. that's why your main domain, it's going to mysite.example.com instead of www.example.com because you have setup your configuration the other way around :D

ServerName mysite.example.com
#ServerAlias mysite.example.com      // not sure if i need alias
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html

<Directory /var/www/html>
 Options Indexes FollowSymLinks MultiViews
 AllowOverride All
 Order allow,deny
 allow from all
</Directory>

ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined

Hope this helped.

  • Hey @Joel Thank you for reply.Actually it was a typo as I did not write the actual configuration.I want both to be pointing to different sites.I have updated the question. – KillABug Jan 2 '16 at 5:55
  • @KillABug Ok, I was suspecting that you had a typo, still wanted to make sure you were the one to clear me out of doubts, alright, now that you have, try setting /var/www/site2 to: /var/www/example.com (assuming it's the main domain) and /var/www/site1 to: /var/www/mysite.example.com. Make sure you make the proper folders for such configuration changes. Also make sure you restart Apache every single time we edit something sudo service apache2 restart – Joel Jan 2 '16 at 6:03
  • yes folders are proper, actually my main domain works fine without www,but with www it fails, now I get a CORS error. I am not sure if it's due to some configuration. as ideally example. com and www.example. com should be same – KillABug Jan 2 '16 at 6:07
  • In your hosts files you should have the entries for each VHost (e.g. www.example.com, another for example.com another for mysite.example.com) and so on. Have you checked your Hosts files regarding this? In such case it may seem that your Hosts file only has the entry for example.com and for mysite.example.com but not for www.example.com – Joel Jan 2 '16 at 6:11
  • I have 2 different conf files for each domain.Each file has one of the virtual host I mentioned in question.I have enabled both sites using a2ensite.Where else do I need to make entry? I believe my entries are right as I tried to point it to a different root with only a single .html file and attaching www to main domain worked there. I think its something at app level. May be angular.js handles requess differently – KillABug Jan 2 '16 at 6:16

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.