sharey Posted June 23, 2007 Share Posted June 23, 2007 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. Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted June 23, 2007 Share Posted June 23, 2007 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 =] Quote Link to comment Share on other sites More sharing options...
sharey Posted June 23, 2007 Author Share Posted June 23, 2007 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 Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted June 23, 2007 Share Posted June 23, 2007 Well, your using the header function wrong. www.php.net/header I have also never seen this "getenv('HTTP_HOST')"....so I couldn't tell you if that was valid or not. Also, your missing a bracket after your "else" clause. Quote Link to comment Share on other sites More sharing options...
sharey Posted June 23, 2007 Author Share Posted June 23, 2007 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, Quote Link to comment Share on other sites More sharing options...
sharey Posted June 23, 2007 Author Share Posted June 23, 2007 Oh, and apparently, getenv() gets an environmental variable such as the current URL. Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted June 23, 2007 Share Posted June 23, 2007 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. Quote Link to comment Share on other sites More sharing options...
JasonO Posted June 23, 2007 Share Posted June 23, 2007 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. Quote Link to comment Share on other sites More sharing options...
sharey Posted June 23, 2007 Author Share Posted June 23, 2007 Thanks all, that helps. I'll get back to you with a success story soon Have a great night. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.