开发者

Using 2 domains on one website

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

There is a primary 开发者_StackOverflowdomain (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.


<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>


Just edit the DNS on domain2.com to point to the same nameserver records for domain1.com. As long as the files used on your site are not hard-coded to a specific domain, it will automatically use masking.


When you say "regular web hosting account", I'm assuming a shared hosting. If so, instead of using the Virtual host feature, for which you may not even have access, you must try the .htaccess method:

There's already a question on this: .htaccess redirect one domain to another incliuding specific query string

Hope it helped. :-)


If you apache server has mod_proxy available, you can use it to pass requests onto other domains by the server - not the client.

You can then make use of it within mod_rewrite RewriteRules by using the P flag.

The benefit is that you can control this per .htaccess:

RewriteRule (.*)\.(jpg|gif|png) http://images.example.com$1.$2 [P]

Simple P / proxy flag example.

You could place a .htaccess inside the document root of domain2 and then proxy all requests over to domain1:

RwriteRule ^(.*)$ http://domain1$1 [P]
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜