elisa Posted June 6, 2007 Share Posted June 6, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/54428-solved-please-help-with-php-redirect-code/ Share on other sites More sharing options...
spooke2k Posted June 6, 2007 Share Posted June 6, 2007 $URL="http://127.0.0.1/teststation.php"; header ("Location: $URL"); think this is what u want spooke2k Quote Link to comment https://forums.phpfreaks.com/topic/54428-solved-please-help-with-php-redirect-code/#findComment-269148 Share on other sites More sharing options...
jscix Posted June 6, 2007 Share Posted June 6, 2007 I believe the $_SERVER array should have your url, from there just some simple parsing and then redirect w/ the code spook gave you. Quote Link to comment https://forums.phpfreaks.com/topic/54428-solved-please-help-with-php-redirect-code/#findComment-269153 Share on other sites More sharing options...
jscix Posted June 6, 2007 Share Posted June 6, 2007 $_SERVER['HTTP_HOST'] to be exact. Quote Link to comment https://forums.phpfreaks.com/topic/54428-solved-please-help-with-php-redirect-code/#findComment-269155 Share on other sites More sharing options...
elisa Posted June 6, 2007 Author Share Posted June 6, 2007 $_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. Quote Link to comment https://forums.phpfreaks.com/topic/54428-solved-please-help-with-php-redirect-code/#findComment-269370 Share on other sites More sharing options...
The Little Guy Posted June 6, 2007 Share Posted June 6, 2007 <?php $URL = $_SERVER['HTTP_HOST']; $NEW = preg_replace("~www.~","",$URL); switch($NEW){ case "http://first.com": header("Location: http://www.first.com/index2.htm"); exit; break; case "http://second.com": header("Location: http://www.second.com/index3.htm"); exit; break; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/54428-solved-please-help-with-php-redirect-code/#findComment-269387 Share on other sites More sharing options...
saf Posted June 6, 2007 Share Posted June 6, 2007 If you do not want the url in the browser to change when you redirect the user, you will need to use something like Apache Mod-Rewrite. Quote Link to comment https://forums.phpfreaks.com/topic/54428-solved-please-help-with-php-redirect-code/#findComment-269390 Share on other sites More sharing options...
elisa Posted June 7, 2007 Author Share Posted June 7, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/54428-solved-please-help-with-php-redirect-code/#findComment-270019 Share on other sites More sharing options...
elisa Posted June 9, 2007 Author Share Posted June 9, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/54428-solved-please-help-with-php-redirect-code/#findComment-271239 Share on other sites More sharing options...
elisa Posted June 10, 2007 Author Share Posted June 10, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/54428-solved-please-help-with-php-redirect-code/#findComment-271998 Share on other sites More sharing options...
elisa Posted June 11, 2007 Author Share Posted June 11, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/54428-solved-please-help-with-php-redirect-code/#findComment-272012 Share on other sites More sharing options...
redarrow Posted June 11, 2007 Share Posted June 11, 2007 apache quistion easer and correct way ok. http://httpd.apache.org/docs/1.3/mod/mod_proxy.html Quote Link to comment https://forums.phpfreaks.com/topic/54428-solved-please-help-with-php-redirect-code/#findComment-272017 Share on other sites More sharing options...
elisa Posted June 11, 2007 Author Share Posted June 11, 2007 apache quistion easer and correct way ok. http://httpd.apache.org/docs/1.3/mod/mod_proxy.html I'm sorry, I have no idea what you are trying to tell me... and your link is a book... I would really like a simple solution, if possible. Thank you. Quote Link to comment https://forums.phpfreaks.com/topic/54428-solved-please-help-with-php-redirect-code/#findComment-272035 Share on other sites More sharing options...
redarrow Posted June 11, 2007 Share Posted June 11, 2007 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] Quote Link to comment https://forums.phpfreaks.com/topic/54428-solved-please-help-with-php-redirect-code/#findComment-272041 Share on other sites More sharing options...
elisa Posted June 11, 2007 Author Share Posted June 11, 2007 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!!! Quote Link to comment https://forums.phpfreaks.com/topic/54428-solved-please-help-with-php-redirect-code/#findComment-272051 Share on other sites More sharing options...
redarrow Posted June 11, 2007 Share Posted June 11, 2007 that a fluke was going just now to do that well done. Quote Link to comment https://forums.phpfreaks.com/topic/54428-solved-please-help-with-php-redirect-code/#findComment-272052 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.