Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using 2 domains on one website

Tags:

url

php

apache

dns

I have a dedicated server (apache, php, mysql)

There is a primary domain (let's call it www.domain1.com) that actually holds all the files like any other regular web hosting account. Another domain (call it domain2.com) needs to forward to it, but with masking.

So domain2.com/filename.php, domain2.com/filename.php/432/r23-gjfdla-fdjslaf/ all need to show the corresponding content of domain1.com's content, BUT the browser should still show domain2.com instead of domain1.com, and it also has to be detectable by $_SERVER['HTTP_HOST'] so my server knows which domain was used to contact the website.

This is because I have 2 clients who are in a partnership, so they would like each visitor to retain whatever URL they entered for independent presentation but make the content unilateral without having to update two sites at once.

like image 856
jeffkee Avatar asked May 31 '11 19:05

jeffkee


People also ask

Can I use 2 domain names for 1 website?

With most registrars, it's easy to forward multiple domains to your website so you can simply create one site and then redirect visitors who type one of your other domain names to that one website.

Can you have 2 domains on the same network?

All on the same physical network in the same building, All absolutely fine. You need to think about how DNS would work here - the DHCP server would need to send a DNS server address to the clients that could serve both domains but that's perfectly do-able.

Is it recommended to register multiple domain names for a website?

Multiple domain names will give people more ways to find your website in a crowded online space and marketplace. Attracting more people to your website helps you build your personal and professional brand. Register.com has the domain registration and online tools you need to build a strong website and online presence.

Is it good to have two domains?

Using multiple domain names can improve your search engine optimization (SEO) results as well. In case you aren't familiar, SEO is a set of strategies designed to help you rank higher and get more visibility in search engine results pages (SERPs).


3 Answers

<VirtualHost *:80>
  DocumentRoot /www/example1
  ServerName www.domain1.com
  ServerAlias www.domain2.com
</VirtualHost>
like image 90
akond Avatar answered Nov 10 '22 03:11

akond


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.

like image 31
Tadeck Avatar answered Nov 10 '22 01:11

Tadeck


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>
like image 23
Mike Avatar answered Nov 10 '22 02:11

Mike