Using 2 domains on one website Using 2 domains on one website apache apache

Using 2 domains on one website


<VirtualHost *:80>  DocumentRoot /www/example1  ServerName www.domain1.com  ServerAlias www.domain2.com</VirtualHost>


What you need is Virtual Host feature - two virtual hosts pointing to one location.

Of course code of the page should be flexible enough to support that - for example internal URLs, if absolute (with http:// or https:// part), should also reflect the changes. But you probably already know it.


I have done something very similar for a couple of small sites run by the same company (one company, two properties, each with their own site). Both are on shared hosting, but you should be able to do exactly the same with VirtualHosts - just define two VirtualHosts, each with a separate domain name, but each pointing to exactly the same document root on the file system:

<VirtualHost *:80>    ServerName site1.com    DocumentRoot /srv/www/public_html</VirtualHost><VirtualHost *:80>    ServerName site2.com    DocumentRoot /srv/www/public_html</VirtualHost>

I have index.php in the public_html directory. This checks $_SERVER['HTTP_HOST'] to determine the domain name that is in use. It then sets a few constants with appropriate directory locations, and a site flag which is used when accessing the database.

I have three directories for static content. One is shared content that is used for both domains, and the other two are site-specific, which include things like logos.

The rest of the PHP scripts are held outside of the document root in a separate scripts directory. Where necessary, the scripts can use the constants defined in index.php, for things such as absolute URLs, or other site-specific information.

/srv/www/||--public_html|  ||  |--site1|  |  ||  |  |--css|  |  |--images|  ||  |--site2|  |  ||  |  |--css|  |  |--images|  ||  |--shared|     ||     |--css|     |--images||--scripts

If you wanted two separate document roots, just create two separate index.php files, one for each. They can then both call the same common codebase (under /srv/www/scripts/, in my case). Something like this:

/srv/www/||--site1|  ||  |--public_html|     ||     |--css|     |--images|   |--site2|  ||  |--public_html|     ||     |--css|     |--images||--scripts

And then:

<VirtualHost *:80>    ServerName site1.com    DocumentRoot /srv/www/site1/public_html</VirtualHost><VirtualHost *:80>    ServerName site2.com    DocumentRoot /srv/www/site2/public_html</VirtualHost>