homepage Welcome to WebmasterWorld Guest from 54.226.235.222
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Forwarding(site) -> "REQUEST URI" ?
Forwarding get url
janton




msg:3788106
 5:55 pm on Nov 17, 2008 (gmt 0)

Hello People,

I'm making a script, but i can't get it to work..!

Situation:

I got a website/domain name that's forwarded to a other domain/host. The url that's typt in the adress bar stays the same and doesn't change.. (because it's some soort of frame)
For example; i go to the website www.example.com, i see the website but the website i see is located at www.example2.com/example, but my adress bar says www.example.com!

Great that's al fine and ok to me!
Buttt the probleme..

Probleme:
I don't want te people to see the website if they go to www.example2.com/example

I thought easy... i make this code that reads out the browser bar adres and if this is nog www.example.com than i redirect him or here to www.example.com!

Well it's not... i made this code down here, but the code only give's me www.example2.com/example everytime and if i enter the site www.example.com it says www.example2.com/example also... but it has to know the url from my adres bar..

Is this Possible?

Code:
__________________________________________________
function curPageURL() {
$pageURL = 'http';
if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
$pageURL .= "://";

if ($_SERVER["SERVER_PORT"] != "80") {
$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].
$_SERVER["REQUEST_URI"];
}

else {
$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
}

return $pageURL;
}

echo curPageURL();

if ( curPageURL() == 'http://www.example.com' ) {
echo "GOOD";
}

if ( curPageURL() == 'http://example.com' ) {
echo "GOOD2";
}

else {
echo "WRONG";
}
__________________________________________________

[edited by: eelixduppy at 6:23 pm (utc) on Nov. 17, 2008]
[edit reason] exemplified [/edit]

 

willis1480




msg:3788224
 8:44 pm on Nov 17, 2008 (gmt 0)

If it is a frame you have a problem. You cannot on the server side determine if the page is loaded to a frame.

However via javascript you can. Add this to the body tag as:

<head>
<script language='javascript'>
function testPage(){
//test for parent frame
if(parent.document){
myObj = parent.document;
}else{
myObj = document;
}
parts = myObj.href.split(".");
test = false;

//cycle through url looking for match
for(i=0;i<parts.length-1;i++){
if(parts[i]+"."+parts[i+1] == "example.com"){
test = true;
}
}
//regardless of framed or directly, redirect to
//example.com
if(!test){myObj.location.href = "http://example.com";}

return;
}
</script>
</head>
<body onLoad='testPage()'>

</body>

Something like that might get you what you want...goodluck!

Dont even have to do the body onLoad...could just run at the top of the html in the head tags.

huperphuff




msg:3788390
 1:04 am on Nov 18, 2008 (gmt 0)

I'm afraid I agree with willis. Php can redirect people only if there are no frames involved. As soon as it's a frame php is powerless to act.

My suggestion?

use some form of willis' code...
and as for the php it looks like you're on the right track.

You might try some debugging techniques...
Print out the array for the $_SERVER variable...

print_r($_SERVER);

I mean it looks like you're using the right variables, but you never know.

But it does look like a frame issue.

once you got the url to forward it to...use the headers to redirect the person.

janton




msg:3788978
 5:30 pm on Nov 18, 2008 (gmt 0)

willis Thx, i thought so to.. After googling for 4 hours.. I was trying java for it.. i have to make my supper now! I take a look at it when i get back!

I hope this works ;)

willis1480




msg:3788988
 5:56 pm on Nov 18, 2008 (gmt 0)

Note about my code: I used example.com as the forward to and as the test subject in the loop. This will keep the page forwarding and reloading itself, please change this for implamentation.

More than likely here is the actual case giving it more though. A frame is unlikely.

More than likely your server is set to forward both URL requests to the same folder:
example.com -> /myfolder/example2/
example2.com -> /myfolder/example2/

This is nice, but will not change the URL. Your origional code in PHP would be the right idea. But to actually forward via PHP, use the header function:
header("location: my_new_URL.com");

janton




msg:3789013
 6:25 pm on Nov 18, 2008 (gmt 0)

Thx willis, I'm back from supper and ready for the challenge!

I see there i made a *BIG* typo in my first post!
And i can't edit it :S

Well this is what i ment:

Probleme:
I don't want te people to see the website if they go to www.example2.com/example

I thought easy... i make this code that reads out the browser bar adres and if this is www.example2.com/example than i redirect him or here to www.example.com!

So you know! Ok i will report my happening!

janton




msg:3789045
 7:02 pm on Nov 18, 2008 (gmt 0)

Huperphuff sorry for my late respons! thx for your reaction to ;)

Ok willis i tryed your code.. only i think, or i do something wrong or there is some code wrong.

This is what i did:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script language='javascript'>
function testPage(){
//test for parent frame
if(parent.document){
myObj = parent.document;
}else{
myObj = document;
}
parts = myObj.href.split(".");
test = false;

//cycle through url looking for match
//WRONG SITE>> IF YOUR BROWSER IS AD EXAMPLE2.COM
for(i=0;i<parts.length-1;i++){
if(parts[i]+"."+parts[i+1] == "example2.com"){
test = true;
}
}
//regardless of framed or directly, redirect to
//example.com
if(!test){myObj.location.href = "http://example.com";}

return;
}
</script>
</head>
<body onLoad='testPage()'>
test
</body>
</html>

So if you enter my website at www.example2.com/example you go to http://example.com...

Also http://example.com is some sort of .tk (but a payed one) , and i can't place code on that site, because it only forwarding mee

Ok i will keep trying!

willis1480




msg:3789054
 7:12 pm on Nov 18, 2008 (gmt 0)

more than likely your server is pointing both:
www.example.com -> /mysites/example2.com/
www.example2.com -> /mysites/example2.com/

you would have to see what the $_SERVER globales will give you. But eitherway, you can should be able to fiture something out that works and uses header() function.

willis1480




msg:3789292
 12:31 am on Nov 19, 2008 (gmt 0)

made a couple mistakes on the javascript...I think this will work:

<script language='javascript'>
function testPage(){
//test for parent frame
if(parent.document.location.href){
myObj = parent.document;
}else{
myObj = document;
}
parts = myObj.location.href.split(".");
test = false;

//cycle through url looking for match
//WRONG SITE>> IF YOUR BROWSER IS AD EXAMPLE2.COM
for(i=0;i<parts.length-1;i++){
if(parts[i]+"."+parts[i+1] == "example2.com"){
test = true;
}
}
//regardless of framed or directly, redirect to
//example.com
if(!test){myObj.location.href = "http://example.com";}

return;
}
</script>

janton




msg:3789760
 5:16 pm on Nov 19, 2008 (gmt 0)

willis YOU are my BEST MEN!
thx.. i got it to work :) :) :) GREAT
Only i have to adjust some parts..
but i think i will get out of it myself :) so nice.. sweat

Willis thx thx thx.. ;)

I have to play with it a little bit and then i hope i get it to work like i want :)

willis1480




msg:3790018
 10:52 pm on Nov 19, 2008 (gmt 0)

good to hear you are on your way

willis1480




msg:3790637
 5:32 pm on Nov 20, 2008 (gmt 0)

Hey, here is a better way of resolving everything...just happened that I was working on this stuff for my own project and wasnt sure yesterday that you could use URL rewriting for forwarding outside your server.

Anyway create a .htaccess file in root folder of domain you want forwarded. Place this into the .htaccess file:

Options +FollowSymlinks
RewriteEngine on
RewriteRule ^(.+) http://www.example.com [R,L]

or use this...301 will tell browsers to permanently redirect:

Options +FollowSymlinks
RewriteEngine on
RewriteRule ^(.+) http://www.example.com [R=301,L]

janton




msg:3790697
 6:52 pm on Nov 20, 2008 (gmt 0)

willis nice .htaccess file..

only when i do that my www.example.com page is empty/blanco
BUT my www.example2.com directly links to www.example.com.. only a empty page isn't so nice ;)

thx again for your time :)

willis1480




msg:3790825
 9:10 pm on Nov 20, 2008 (gmt 0)

Try this RewriteRule
RewriteCond %{HTTP_HOST} ^example2\.com [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R,NC]

if this does the trick, change the R to R=301 (only if it works!)

janton




msg:3796541
 12:17 pm on Nov 29, 2008 (gmt 0)

sorry for the late late respons.. I tryed your last rewriterule but also no effect.. i tryed it a week ago, not sure anymore what happend but i can't test it now, got some visiters on my website ;)
i will try it tomorrow night again.. i'm not sure anymore but i think it resulted in a blanco screen to.

Still working with the old java script from above:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script language='javascript'>
function testPage(){
//test for parent frame
if(parent.document.location.href){
myObj = parent.document;
}else{
myObj = document;
}
parts = myObj.location.href.split(".");
test = false;

//cycle through url looking for match
//WRONG SITE>> IF YOUR BROWSER IS AD EXAMPLE2.COM
for(i=0;i<parts.length-1;i++){
if(parts[i]+"."+parts[i+1] == "example2.com"){
test = true;
}
}
//regardless of framed or directly, redirect to
//example.com
if(!test){myObj.location.href = "http://example.com";}

return;
}

</script>
</head>
<body onLoad='testPage()'>
</body>
</html>

So this .html file contains the javascript.
I wanted to add this in a .js file .. because my website first loads totaly and then after about 4 seconds it redirects..
I thought maby because my php is parsed or read first by the host.. and html is read as last?

Well the thing is, how can i put this script into a .js file and excute(before the php).. because you also have to put <body onLoad='testPage()'> in the html.. ok i try looking for it!

willis1480




msg:3796745
 10:57 pm on Nov 29, 2008 (gmt 0)

you cannot run any js or HTML prior to parsing the PHP. Just PM me your web address so I can see what is really going on with your redirect, hosting. using .htaccess and mod rewrite is the best solution.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved