Page 1 of 1
Please help me with PHP redirect code
Posted: Wed Jun 06, 2007 9:03 am
by elisa
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
http://www.first.com I want the code to redirect to
http://www.first.com/index2.htm
If they type
http://www.second.com I want the code to redirect to
http://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
Posted: Wed Jun 06, 2007 9:09 am
by feyd
What do you have so far?
Posted: Wed Jun 06, 2007 1:14 pm
by RobertGonzalez
Can you do this at the domain level? I know with godaddy you can set a domain forward instruction for a particular domain name and retain the entered URL in the address bar.
Posted: Wed Jun 06, 2007 1:26 pm
by elisa
I can't with this hosting service (I wish!).
This is what I have so far, but it doesn't really matter because it's not working...
Code: Select all
<?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');
}
?>
Thank you. E.
Posted: Wed Jun 06, 2007 1:31 pm
by RobertGonzalez
I think you are at the very least going to need to have a .htaccess file somewhere that will route all incoming requests to a particular page, then that page would have to handle the redirects. Normally I would say do this on the server, but since you don't have access, you will need to do this with .htaccess.
My next question is how do you have your server configured at the moment? Are requests to first.com and second.com handled on each virtual server or is there a single server that is serving all requests to both those domains?
Posted: Thu Jun 07, 2007 9:59 am
by elisa
Hi,
sorry for the confusion, I guess this was important to specify:
Right now the server is hosting first.com so any request for first.com goes to first.com/index.php
I've then created and 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.
Elisa
Posted: Fri Jun 08, 2007 2:23 pm
by elisa
can anybody tell me where is my code wrong?
thank you,
Elisa
Posted: Fri Jun 08, 2007 3:20 pm
by RobertGonzalez
Make a really quick addition to your index.php to include this:
Code: Select all
<?php
echo '<!-- URI is ' . $_SERVER['REQUEST_URI'] . ' -->';
?>
Call both domains and view the source and see what the source shows for this value. Post back your findings for each domain call.
Posted: Fri Jun 08, 2007 11:14 pm
by elisa
feyd | Please use Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
I've added your suggestion and when I type http://www.first.com I get
<!-- URI is -->
in the source. That's all.
Same exact thing when I type http://www.second.com
This is what my index.php is looking like right now (I've tried changing the "URI" to "URL" but nothing changes):
Code: Select all
<?php
echo '<!-- URI is ' . $_SERVER['REQUEST_URI'] . ' -->';
if ($REQUEST_URI == "http://www.first.com")
{
header('location: http://www.first.com/index2.htm');
}
else if ($REQUEST_URI == "http://www.second.com")
{
header('location: http://www.second.com/index3.htm');
}
?>
Please, remember that first.com is the original domain. Second.com is aliasing right now to first.com. So without a redirect page, both domains are going to the same index page. I need them to both hit index.php and then take separate paths maintaining the domain they have entered index.php with.
Thanks again for your help.
feyd | Please use Code: Select all
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Posted: Sat Jun 09, 2007 10:31 am
by RobertGonzalez
Can you make a PHP info page for first.com and report back the $_SERVER and/or $_ENV section of the output?
Posted: Sun Jun 10, 2007 6:06 pm
by elisa
Hi Everah,
thank you very much for your time on this.
I did a phpinfo and these are the results:
Code: Select all
_SERVER["HTTP_CONNECTION"] Keep-Alive, keep-alive
_SERVER["HTTP_PROXY"] xxx.xxx.xxx.xxx
_SERVER["HTTP_PROXY_IP"] xx.xxx.xxx.xx
_SERVER["HTTP_HOST"] www.xxxxxx.com
_SERVER["HTTP_USER_AGENT"] Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.4) Gecko/20070515 Firefox/2.0.0.4
_SERVER["HTTP_ACCEPT"] text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
_SERVER["HTTP_ACCEPT_LANGUAGE"] en-us,en;q=0.5
_SERVER["HTTP_ACCEPT_ENCODING"] gzip,deflate
_SERVER["HTTP_ACCEPT_CHARSET"] ISO-8859-1,utf-8;q=0.7,*;q=0.7
_SERVER["HTTP_KEEP_ALIVE"] 300
_SERVER["HTTP_X_CH_INDEX_FILE"] index.php
_SERVER["TZ"] US/Eastern
_SERVER["SERVER_SOFTWARE"] ConcentricHost-Ashurbanipal/1.8 (Concentric(R))
_SERVER["SERVER_NAME"] www.xxxxx.com
_SERVER["SERVER_ADMIN"] www@www.xxxxx.com
_SERVER["SERVER_PORT"] 80
_SERVER["REMOTE_HOST"] cpe-xx-xxx-xxx-xx.nyc.res.rr.com
_SERVER["REMOTE_ADDR"] xx.xxx.xxx.xx
_SERVER["DOCUMENT_ROOT"] /web
_SERVER["CB_SLOT"] 0
_SERVER["CB_CODE"] 5
_SERVER["GATEWAY_INTERFACE"] CGI/1.1
_SERVER["SERVER_PROTOCOL"] HTTP/1.0
_SERVER["REQUEST_METHOD"] GET
_SERVER["SCRIPT_NAME"] /index.php
_SERVER["QUERY_STRING"] no value
_SERVER["MANPATH"] /usr/man
_SERVER["PATH"] /bin:/usr/vde/bin:/usr/local/bin:/usr/gnu/bin:/usr/ucb:/usr/ccs/bin:/usr/local/cvbin
_SERVER["USER"] cgiuser
_SERVER["LOGNAME"] cgiuser
_SERVER["SHELL"] /usr/local/pkg/php4/bin/php
_SERVER["LOCALDOMAIN"] xxxxxx.com
_SERVER["VDE_DOMAIN"] xxxxxx.com
_SERVER["VDE_ZONE"] /cgi/
_SERVER["VDE_LOGNAME"] cgiuser@xxxxxx.com
_SERVER["VDE_INBOUND"] vrun:httpd
_SERVER["VDE_VERSION"] 1.9d (clock)
_SERVER["VDE_PGRPID"] 13622
_SERVER["HOME"] /
_SERVER["PWD"] /web
_SERVER["VDE_CB_GRENT"] chost
_SERVER["VDE_CB_PROD_NAME"] Concentric(R)
_SERVER["VDE_CB_TLD"] concentric.com
_SERVER["VDE_CB_SIGNUP_PATH"] CONCENTRIC
_SERVER["SCRIPT_FILENAME"] /web/index.php
_SERVER["PHP_SELF"] /index.php
_SERVER["argv"]
Array
(
)
_SERVER["argc"] 0
_ENV["HTTP_CONNECTION"] Keep-Alive, keep-alive
_ENV["HTTP_PROXY"] xxx.xxx.xxx.xxx
_ENV["HTTP_PROXY_IP"] xx.xxx.xxx.xx
_ENV["HTTP_HOST"] www.xxxxx.com
_ENV["HTTP_USER_AGENT"] Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.4) Gecko/20070515 Firefox/2.0.0.4
_ENV["HTTP_ACCEPT"] text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
_ENV["HTTP_ACCEPT_LANGUAGE"] en-us,en;q=0.5
_ENV["HTTP_ACCEPT_ENCODING"] gzip,deflate
_ENV["HTTP_ACCEPT_CHARSET"] ISO-8859-1,utf-8;q=0.7,*;q=0.7
_ENV["HTTP_KEEP_ALIVE"] 300
_ENV["HTTP_X_CH_INDEX_FILE"] index.php
_ENV["TZ"] US/Eastern
_ENV["SERVER_SOFTWARE"] ConcentricHost-Ashurbanipal/1.8 (Concentric(R))
_ENV["SERVER_NAME"] www.xxxxx.com
_ENV["SERVER_ADMIN"] www@www.xxxxx.com
_ENV["SERVER_PORT"] 80
_ENV["REMOTE_HOST"] cpe-xx-xxx-xxx-xx.nyc.res.rr.com
_ENV["REMOTE_ADDR"] xx.xxx.xxx.xx
_ENV["DOCUMENT_ROOT"] /web
_ENV["CB_SLOT"] 0
_ENV["CB_CODE"] 5
_ENV["GATEWAY_INTERFACE"] CGI/1.1
_ENV["SERVER_PROTOCOL"] HTTP/1.0
_ENV["REQUEST_METHOD"] GET
_ENV["SCRIPT_NAME"] /index.php
_ENV["QUERY_STRING"] no value
_ENV["MANPATH"] /usr/man
_ENV["PATH"] /bin:/usr/vde/bin:/usr/local/bin:/usr/gnu/bin:/usr/ucb:/usr/ccs/bin:/usr/local/cvbin
_ENV["USER"] cgiuser
_ENV["LOGNAME"] cgiuser
_ENV["SHELL"] /usr/local/pkg/php4/bin/php
_ENV["LOCALDOMAIN"] xxxxx.com
_ENV["VDE_DOMAIN"] xxxxx.com
_ENV["VDE_ZONE"] /cgi/
_ENV["VDE_LOGNAME"] cgiuser@xxxxx.com
_ENV["VDE_INBOUND"] vrun:httpd
_ENV["VDE_VERSION"] 1.9d (clock)
_ENV["VDE_PGRPID"] 13622
_ENV["HOME"] /
_ENV["PWD"] /web
_ENV["VDE_CB_GRENT"] chost
_ENV["VDE_CB_PROD_NAME"] Concentric(R)
_ENV["VDE_CB_TLD"] concentric.com
_ENV["VDE_CB_SIGNUP_PATH"] CONCENTRIC
_ENV["SCRIPT_FILENAME"] /web/index.php
I hope this is what you were asking for. I've x'd out the domain name, but it's always first.com on this list (never second.com), just in case you need that info.
Thanks a million,
Elisa
Posted: Sun Jun 10, 2007 7:35 pm
by elisa
I've tried something else:
Code: Select all
<?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
http://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).
Also, remember that I don't know PHP so understand if my syntax is completely wrong.
Thanks.
ps. I'm about ready to give up...
