Jump to content

Redirect to specific page


sharey

Recommended Posts

Hi, simple question - just dont know the simple answer.

 

I have 2 domain names that point to the exact same directory on a webserver

 

say.. www.dn1.com and www.dn2.com

 

when the user comes from www.dn2.com id like to redirect to page2.php, and if they go to www.dn1.com just go to index.php. So, how can i look at the refering url and then redirect based on it?

 

should be easy, right? I just have no idea what im doing with PHP, but im a fast learner!

 

Thanks in advanced - you're cool.

 

 

Link to comment
https://forums.phpfreaks.com/topic/56802-redirect-to-specific-page/
Share on other sites

Okay, I'm going to give this a shot.

 

<?php

$refer = $_SERVER['HTTP_REFERER'];

if ($refer == "http://www.dn2.com"){
   header('site.com/page2.php');

} else if ($refer == "http://www.dn1.com"){
   header('site.com/index.php');  
}

?>

 

I have no idea if it will work, a complete shot in the dark. You can try it out though =]

thanks, Ive tired the following and it doesnt work (just made it up, have no idea what im doing).

 

<?PHP

$ref = getenv('HTTP_HOST');

echo $ref;

 

if($ref == 'dn2.com')  {

header ('http://www.google.com')

} else

header ('http://www.yahoo.com')

}

?>

 

does this go in the header? I'll try yours too

 

Good calls.

In that case, are you using it wrong as well since you don't have "Location" in there? Also, it mentions header must be used before any output... is this referring to output within the body tags? If so, am I safe by putting this in the head tags - is that the right spot?

 

I'm pretty sure I had that curly bracket in there, but bumped it before i posted for you.

 

Anyway, I'll try again soon, but I'm on my laptop now and don't know the server credentials to test right now :/

 

Hope to hear back though.

 

Thanks,

 

 

Woah 0_o I have nooo idea how I forgot to put the "location" in the header tag, stupid me.

 

<?php

$refer = $_SERVER['HTTP_REFERER'];

if ($refer == "http://www.dn2.com"){
   header('Location: site.com/page2.php');

} else if ($refer == "http://www.dn1.com"){
   header('Location: site.com/index.php');  
}

?>

 

And no, don't put this code in the head tag, this should go in between the body tags with the rest of you code.

 

 

In that case, are you using it wrong as well since you don't have "Location" in there? Also, it mentions header must be used before any output... is this referring to output within the body tags? If so, am I safe by putting this in the head tags - is that the right spot?

 

Yep Location needs to be used, see below. Header must be used before <html> or it will give you an error. The first line should be the start of your header information, and after that your normal HTML tags etc would be used.

 

<?php
header('Location: http://www.example.com/');
?>
<html>

 

http://uk3.php.net/header - Says that if it is put AFTER the <html> tag, it will give an error.

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.