Forum Moderators: coopster
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]
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.
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.
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");
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!
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!
<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>
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]
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!