1

I was wondering if im doing something wrong or what ? I have subdomain pointing to my server IP ( public ip of my server ) but when ever i go to my subdomain it show me the default page of Apache. Yes I'm using name virtual hosts but still nothing ...

For now i have put the page in the html directory, and it works ok but i think that is not the solution. And yes i can point my subdomain to subdomain.domain.com/thepage but this i don't want to do. And here is what i have in conf. part

    NameVirtualHost *:80
    <VirtualHost *:80>
    ServerName    subdomain.domain.com
    DocumentRoot  /var/www/
    ServerAlias   www.subdomain.domain.com
    </VirtualHost>

Any suggestion ?


This is what i've got ..

NameVirtualHost *:80
<VirtualHost *:80>
ServerName    domain.com
DocumentRoot  /var/www/ - here is domain.com ( there is directory named ivan )
ServerAlias   www.domain.com
</VirtualHost>

NameVirtualHost *:80
<VirtualHost *:80>
ServerName    subdomain1.domain.com
DocumentRoot  /var/www/ - and here is subdomain1.domain.com - directory named sub1
ServerAlias   www.subdomain1.domain.com
</VirtualHost>

NameVirtualHost *:80
<VirtualHost *:80>
ServerName    subdomain2.domain.com
DocumentRoot  /var/www/ - and here is subdomain2.domain.com - directory named sub2 ( And I don't know if they need to be the same name as the address or ??? sometimes I'm confused about that. )
ServerAlias   www.subdomain2.domain.com
</VirtualHost>

And all i got is default page of Apache ...

Stefan Lasiewski
  • 23,667
  • 41
  • 132
  • 186
IvanAK
  • 147
  • 1
  • 1
  • 11
  • Are you sure you want to have the same `DocumentRoot` as the main domain ? (I assume that `/var/www` is used by your main domain also) – b13n1u Feb 02 '14 at 19:54
  • Not sure I understand what you are saying: when you browse to "subdomain.domain.com" you are not getting the files from /var/www ? – ETL Feb 02 '14 at 20:41
  • ETL- Yes .. when i go to subdomain.domain.com i get the default page from apache. b13n1u - I like, when i go to subdomain.domain.com to show me the page that is in /var/www/page1 ( for example ) and when i go to subdomain2.domain.com or domain.com to show me the page that is in /var/www/page2 etc ... – IvanAK Feb 02 '14 at 20:56
  • 1
    For subdomain.domain.com you should set the DocumentRoot of the subdomain to `DocumentRoot /var/www/page1` and for subdomain2.domain.com `DocumentRoot /var/www/page2` Otherwise your subdomain will show exactly the same data as your main domain. Check out the [examples](http://httpd.apache.org/docs/current/vhosts/examples.html) – b13n1u Feb 02 '14 at 21:54
  • @b13n1u That sounds like the answer; you should probably go below and make it an answer. – Michael Hampton Feb 02 '14 at 21:58
  • probably you have missed "sudo service apache2 restart" – Dhananjay Yadav Aug 10 '20 at 12:33

2 Answers2

2

You need to modify the root directory for your virtual host. Otherwise you will have a subdomain which will show exactly the same data as your main domain.

For subdomain.domain.com you should set the DocumentRoot of the subdomain to DocumentRoot /var/www/page1 and for subdomain2.domain.com to DocumentRoot /var/www/page2 etc.

Check out the examples

b13n1u
  • 980
  • 9
  • 14
1

Your configuration should look like this:

NameVirtualHost *:80

<VirtualHost *:80>
  ServerName    domain.com
  ServerAlias   www.domain.com
  DocumentRoot  /var/www/ivan # absolute path to your web root
</VirtualHost>

<VirtualHost *:80>
  ServerName    subdomain1.domain.com
  ServerAlias   www.subdomain1.domain.com
  DocumentRoot  /var/www/sub1 # absolute path to your web root
</VirtualHost>

<VirtualHost *:80>
  ServerName    subdomain2.domain.com
  ServerAlias   www.subdomain2.domain.com
  DocumentRoot  /var/www/sub2 # absolute path to your web root
</VirtualHost>
roman
  • 111
  • 4