Jump to content

[SOLVED] Please help with PHP redirect code


elisa

Recommended Posts

Hi,

I have tried to do this myself but I can't make it work.

 

I have two domains, first.com and second.com, but one hosting service.

 

I want my index.php to be just the redirect to one site or the other, depending what has been typed in the url.

 

If they type www.first.com I want the code to redirect to www.first.com/index2.htm

 

If they type www.second.com I want the code to redirect to www.second.com/index3.htm

 

This also so that whatever domain they have typed will stay on the address bar. I don't want the two sites mixed up.

 

Can someone please help me?

 

I know this can be done with htaccess, but I don't have access to the root of the server.

 

I've also tried with javascript, but I couldn't get it to work either.

 

I'm more of a webdesigner, not really a raw code girl  :(

 

Thank you in advance,

Elisa

Link to comment
Share on other sites

$_SERVER['HTTP_HOST']

 

to  be exact.

 

This is what I have now:

 

<?php

 

if  ($REQUEST_URL  ==  "http://www.first.com"

 

{

 

    header('location:  http://www.first.com/index2.htm');

 

 

    else  if  ($REQUEST_URL  ==  "http://www.second.com"

 

{

 

    header('location:  http://www.second.com/index3.htm');

}

?>

 

And it doesn't work. How would I change it to include your suggestion?

 

Thank you. E.

Link to comment
Share on other sites

Thank you I will try that now.

 

I've forgot to specify that right now the server is hosting first.com so any request for first.com goes directly to first.com/index.php

 

Then I've created an alias for second.com to go to first.com, so any requests for second.com will also

go to first.com/index.php

 

So the index.php is the redirect file that I'm trying to code correctly.

 

Will that code you provided still be applicable?

 

Thank you,

 

Elisa

Link to comment
Share on other sites

I've tried the code little guy gave me, but still no luck. No matter which domain I try the page goes blank and it doesn't redirect.

 

What am I doing wrong?

 

Do I need to change anything on this line? Do I need to replace server with anything?

 

$URL = $_SERVER['HTTP_HOST'];

 

Just checking...

 

Thanks again.

Link to comment
Share on other sites

Can anybody else help me with this?

 

You can also tell me that what I am asking for is not possible (but I doubt it).

 

Please understand that I am very new at PHP, actually I know nothing about it. So if anybody can guide me through this, I would really appreciate it.

 

Thank you,

Elisa

Link to comment
Share on other sites

I've tried something else:

 

<?php
if ($_SERVER['HTTP_HOST']="www.first.com") {
header("location: http://www.first.com/index2.htm");
}  else if ($_SERVER['HTTP_HOST']="www.second.com") {
header("location: http://www.second.com/index3.htm");
}  
?>

 

Now what happens here is no matter if I type first.com or second.com it goes to www.first.com/index2.htm.

I guess it doesn't read the second part of the redirect.

Please remember that right now second.com is an alias domain name, that is directed at first.com.

So how can I make that second part work and have anything directed at second.com redirect to second.com/index3.htm? (index2.htm and index3.htm are in the same folder, because of the aliasing).

 

Thanks.

Link to comment
Share on other sites

give this a go fingers crossed.

[code]
<?php

$url="www.first.com";

if ($_SERVER['HTTP_HOST']==$url) {

header("location: http://www.first.com/index2.htm");

}  else if (!$url) {

header("location: http://www.second.com/index3.htm");

}  
?>

 

 

or this

 

<?php

$url="www.first.com";

if ($_SERVER['HTTP_HOST']==$url) {

header("location: http://www.first.com/index2.htm");

}  

if (!$url) {

header("location: http://www.second.com/index3.htm");

}  
?>

[/code]

Link to comment
Share on other sites

OMG!!! Redarrow I love you!!!

 

The first code didn't work, but the second got me thinking... I changed it slightly to this:

 

<?php

$url="www.first.com";

if ($_SERVER['HTTP_HOST']==$url) {

header("location: http://www.first.com/index2.htm");

}  else {

header("location: http://www.second.com/index3.htm");

}  
?>

 

Thank you, thank you, thank you!!!  :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.