2 Domains, 1 Site, load different index files

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
DSchragg
Forum Newbie
Posts: 4
Joined: Tue Jul 27, 2010 6:57 pm

2 Domains, 1 Site, load different index files

Post 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.
buckit
Forum Contributor
Posts: 169
Joined: Fri Jan 01, 2010 10:21 am

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

Post 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
DSchragg
Forum Newbie
Posts: 4
Joined: Tue Jul 27, 2010 6:57 pm

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

Post 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....
buckit
Forum Contributor
Posts: 169
Joined: Fri Jan 01, 2010 10:21 am

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

Post 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>";
}
buckit
Forum Contributor
Posts: 169
Joined: Fri Jan 01, 2010 10:21 am

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

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

DSchragg
Forum Newbie
Posts: 4
Joined: Tue Jul 27, 2010 6:57 pm

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

Post by DSchragg »

This worked great, thanks again for your help!
internet-solution
Forum Contributor
Posts: 220
Joined: Thu May 27, 2010 6:27 am
Location: UK

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

Post 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:
DSchragg
Forum Newbie
Posts: 4
Joined: Tue Jul 27, 2010 6:57 pm

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

Post by DSchragg »

I used the PHP option, and everything worked out great......thanks again. I didn't expect an answer so quickly.......
User avatar
greyhoundcode
Forum Regular
Posts: 613
Joined: Mon Feb 11, 2008 4:22 am

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

Post by greyhoundcode »

If your site doesn't require PHP and is composed of static pages, perhaps this could be accomplished also with .htaccess rules.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

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

Post 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.
There are 10 types of people in this world, those who understand binary and those who don't
Post Reply