Vishal Kumar Vishal Kumar - 12 days ago 7
PHP Question

Apache Virtual host Point 2 domains to same root with different index

I am new to VPS hosting.... and giving a try to digital ocean Linux VPS. I need to point 2 domains to my PHP app. That is located at var/www/html

I want my first domain to point to the root so that it can launch index.php by default.

I also have a login.php page in the same document root...and want my second domain to directly point to this file...e.g. var/www/html/login.php

This is how my Virtual Host for domain1 looks like inside /etc/apache2/sites-available

<VirtualHost *:80>
ServerAdmin root@domain1.com
ServerName domain1.com
ServerAlias www.domain1.com
DocumentRoot /var/www/html
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>


this works great for both domains but when I am trying to change the line

DocumentRoot /var/www/html/login.php


The second domain is not loading css and js files for the page. I am not sure what can be an easy and safe way to do this.

Answer

In your second VH definition you could try changing the DirectoryIndex to point to the login script like this

<VirtualHost *:80>
    ServerAdmin root@domain2.com
    ServerName domain2.com
    ServerAlias www.domain2.com
    DocumentRoot /var/www/html

    DirectoryIndex login.php

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