Click to See Complete Forum and Search --> : PHP redirect


MarySunshine
10-28-2003, 06:33 PM
I use this code in an ASP file on another server to redirect domains to various files/directories. I want to do the same thing in PHP. I have done a search on "PHP redirect" and have found all kinds of stuff but NOT what I am looking for. :confused: Can anyone help me here?
This is the script:

<%
dim serverName
serverName = request.ServerVariables("SERVER_NAME")
if inStr(1,serverName,"domain1.com",1)>0 then
response.Redirect("/domain1")
elseif inStr(1,serverName,"domain2.com",1)>0 then
response.Redirect("/domain2")
elseif inStr(1,serverName,"domain3.com",1)>0 then
response.Redirect("/domain3")
elseif inStr(1,serverName,"domain4",1)>0 then
response.Redirect("welcome.htm")
else
response.Write("You came to unknown domain. Please choose one of the following:<br>")
response.Write("<a href='http://www.domain1.com'>http://www.domain1.com</a><br>")
response.Write("<a href='http://www.domain2.com'>http://www.domain2.com</a><br>")
response.Write("<a href='http://www.domain3.com'>http://www.domain3.com</a><br>")
response.Write("<a href='http://www.domain4.com'>http://www.domain4.com</a><br>")
end if
%>

PunkSktBrdr01
10-28-2003, 07:14 PM
Try this:


<?
$server_name = $_SERVER['SERVER_NAME'];

if (substr_count($server_name, "domain1.com") > 0) {
header("Location: /domain1");
}
elseif (substr_count($server_name, "domain2.com") > 0) {
header("Location: /domain2");
}
elseif (substr_count($server_name, "domain3.com") > 0) {
header("Location: /domain3");
}
elseif (substr_count($server_name, "domain4.com") > 0) {
header("Location: /domain4");
}
else {
echo"You came to unknown domain. Please choose one of the following:<br>\n";
echo"<a href=\"http://www.domain1.com\">http://www.domain1.com</a><br>\n";
echo"<a href=\"http://www.domain2.com\">http://www.domain2.com</a><br>\n";
echo"<a href=\"http://www.domain3.com\">http://www.domain3.com</a><br>\n";
echo"<a href=\"http://www.domain4.com\">http://www.domain4.com</a><br>\n";
}
?>

MarySunshine
10-28-2003, 07:43 PM
Thank you! That seems to work... EXCEPT when I name it index.php. For some reason, the browser won't open that file when sent to www.domain1.com

I thought www.domain1.com would open up www.domain1.com/index.php

Any ideas why it isn't working?

PunkSktBrdr01
10-28-2003, 09:03 PM
Not sure. Maybe, because you are redirected specifically to "www.domain1.com", it isn't working right. Try adding a slash at the end, like "www.domain1.com/". That might work.

MarySunshine
10-28-2003, 09:45 PM
I don't think that's it. The server replied with a file not found when www.domain1.com was entered. So the file didn't even get called upon. Can't figure out why!?!?

PunkSktBrdr01
10-29-2003, 06:50 AM
Why are you doing redirects this way, though? If the domain is already pointing to your server, which it must be, why not just have it point to the specific folder? That would be faster and more efficient. Anyway, when you have:


else {

echo"You came to unknown domain. Please choose one of the following:<br>\n";

echo"<a href=\"http://www.domain1.com\">http://www.domain1.com</a><br>\n";

echo"<a href=\"http://www.domain2.com\">http://www.domain2.com</a><br>\n";

echo"<a href=\"http://www.domain3.com\">http://www.domain3.com</a><br>\n";

echo"<a href=\"http://www.domain4.com\">http://www.domain4.com</a><br>\n";

}


you should change the hrefs to the actual folder location instead of forcing the script to run again. That would just be a waste of resources.