Page 1 of 1

2 Domains, 1 Site, load different index files

Posted: Tue Jul 27, 2010 6:59 pm
by DSchragg
Hello Everyone, this is my first post on this great and very useful site.

I'm hoping someone can give me a quick and simple answer for this one.....

Here's my scenario:

I have 2 domain names that point to one website. One domain is english the other french. I want an index.php file to handle what index file is loaded.
If the user types in french.com I want it to load the index_french.html file, if the user types english.com I want the site to load index.html

Sounds simple enough, but I may be wrong?

Thanks in advance for your help.

Re: 2 Domains, 1 Site, load different index files

Posted: Tue Jul 27, 2010 7:23 pm
by buckit
a very basic way of doing it:

Code: Select all

if($_SERVER['SERVER_NAME'] == 'french.mysite.com'){
header('Location: http://french.mysite.com/index_french.html');
}
put that at the top if index.PHP

make index.php your English version.

if they used the French domain name then they will be redirected to index_french.html. if not then it will continue loading index.php

Re: 2 Domains, 1 Site, load different index files

Posted: Tue Jul 27, 2010 7:29 pm
by DSchragg
Thank you for your quick response.....

I should have pointed out that my site does not utilize PHP at all the site is all static HTML pages. Really I just wanted to implement a PHP file to handle the different domain names, so I would assume an index.php file created with this rule for each domain should do the trick?

I'm going to test this now and report back.

Thanks again....

Re: 2 Domains, 1 Site, load different index files

Posted: Tue Jul 27, 2010 7:37 pm
by buckit
yeah... for the most part

you could do this:

Code: Select all

if($_SERVER['SERVER_NAME'] == 'french.mysite.com'){
    header('Location: http://french.mysite.com/index_french.html');
}else if($_SERVER['SERVER_NAME'] == 'english.mysite.com'){
    header('Location: http://english.mysite.com/index.html');
}else{
    echo "Please choose a language.  <a href='/index.html'>English</a> or <a href='/index_french.html'>French</a>";
}

Re: 2 Domains, 1 Site, load different index files

Posted: Tue Jul 27, 2010 7:42 pm
by buckit
or you could skip PHP all together and use javascript

Code: Select all

<script>
if(window.location.hostname == 'french.mysite.com'){
    window.location = 'http://french.mysite.com/index_french.html';
}
</script>


Re: 2 Domains, 1 Site, load different index files

Posted: Tue Jul 27, 2010 7:45 pm
by DSchragg
This worked great, thanks again for your help!

Re: 2 Domains, 1 Site, load different index files

Posted: Tue Jul 27, 2010 9:10 pm
by internet-solution
buckit wrote:or you could skip PHP all together and use javascript

Code: Select all

<script>
if(window.location.hostname == 'french.mysite.com'){
    window.location = 'http://french.mysite.com/index_french.html';
}
</script>

Whats happens if the user blocks javascript???? :twisted:

Re: 2 Domains, 1 Site, load different index files

Posted: Tue Jul 27, 2010 9:21 pm
by DSchragg
I used the PHP option, and everything worked out great......thanks again. I didn't expect an answer so quickly.......

Re: 2 Domains, 1 Site, load different index files

Posted: Wed Jul 28, 2010 3:41 am
by greyhoundcode
If your site doesn't require PHP and is composed of static pages, perhaps this could be accomplished also with .htaccess rules.

Re: 2 Domains, 1 Site, load different index files

Posted: Wed Jul 28, 2010 4:36 am
by VladSun
http://corz.org/serv/tricks/htaccess2.php
multiple domains in one root
If you are in the unfortunate position of having your sites living on a host that doesn't support multiple domains, you may be forced to roll your own with .htaccess and mod_rewrite. So long as your physical directory structure is well thought-out, this is fairly simple to achieve.

For example, let's say we have two domains, pointing at a single hosted root; domain-one.com and domain-two.com. In our web server root, we simply create a folder for each domain, perhaps one/, and two/ then in our main (root) .htaccess, rewrite all incoming requests, like this..

All requests NOT already rewritten into these folders, transparently rewrite..
#two domains served from one root..
RewriteCond %{HTTP_HOST} domain-one.com
RewriteCond %{REQUEST_URI} !^/one
RewriteRule ^(.*)$ one/$1 [L]

RewriteCond %{HTTP_HOST} domain-two.com
RewriteCond %{REQUEST_URI} !^two
RewriteRule ^(.*)$ two/$1 [L]

All requests for the host domain-one.com are rewritten (not R=redirected) to the one/ directory, so long as they haven't already been rewritten there (the second RewriteCond). Same story for domain-two.com. Note the inconsistency in the RewriteCond statement; !^/dir-name and !^dir-name should both work fine.

Also note, with such a simple domain & folder naming scheme, you could easily merge these two rule sets together. This would be unlikely in the real world though, which is why I left them separate; but still, worth noting.

Other general settings and php directives can also go in this root .htaccess file, though if you have any further rewrite you'd like to perform; short URL's, htm to php conversion and what-not; it's probably easier and clearer to do those inside the sub-directory's .htaccess files.