Forum Moderators: coopster
I'm sure a PHP expert in here could figure this one out.
I'm trying to create a PHP redirect but the redirect is based on a query string (http://mydomain.com/?query_string).
So let's say my original PHP redirect code (redirect.php) looks like this:
<?php
header("Location: http://mydomain.com/page.html");
?>
I want to be able to type into the browser or click on this link: 'http://mydomain.com/redirect.php?order' and go here: 'http://mydomain.com/order.html
So I could type in anything as the query string and the redirect file would redirect me to the appropriate page:
[mydomain.com...]
[mydomain.com...]
[mydomain.com...]
would redirect me respectively to...
[mydomain.com...]
[mydomain.com...]
[mydomain.com...]
If I have the answer to my question above, I guess I can take that and easily apply it to redirection to sub directories also right?
So...
[mydomain.com...]
would redirect me to...
Any help is appreciated.
Thanks!
Nguyen
header("Location: http://www.example.com/".$_GET["url"]);
exit;
With the url being something like this:
http://www.example.com/redirect.php?url=page1.html
Good luck!
How would it work with sub directories?
I'm looking to insert a text string in the:
<?php
header("Location: http://query.mydomain.com");
?>
vincevincevince:
I have something like this:
<?php
$var = $_SERVER['QUERY_STRING'];
?>
Okay, I know I have to insert the following where I want my redirect to go but I don't know how. I think I'm not getting the syntax correct. I know I'm not getting it right because I'm not a programmer...hehe.
If it was an ordinary link all I had to do is...
<a href="<?php if(empty($var)) { echo "subdomain"; } else echo "$var"; }?>.mydomain.com">Click Here!</a>
but how would I apply this in a PHP redirect with..
<?php
header("Location: http://query.mydomain.com");
?>
$url = $_GET["url"];
if(isset($_GET["sub"])) {
header("Location: http://".$url.".example.com");
exit;
}
else {
header("Location: http://www.example.com/".$url);
exit;
}
Now for a regular redirect, you use this query:
http://www.example.com/redirect.php?url=page.html
For a subdomain redirect, you use this:
http://www.example.com/redirect.php?url=domain&sub=1
I hope this is what you need. Good luck!
Would it redirect to a different domain? Like to [subdomain.yourdomain.com?...]
$url = $_GET["url"];
header("Location: ".$url);
exit;
that way, no matter what url you want to go to, you go there like this:
http://www.example.com/redirect.php?url=http://www.google.com
Good luck!
Okay, I got it figured out...not too bad for someone who doesn't know HTML or programming.
Thanks to people like you who point me to the right direction.
I took bits and pieces of codes and here is what I got:
<?php
$var = $_SERVER['QUERY_STRING'];
{
header("Location: [".$var.".mydomain.net");...]
exit;
}
?>
So I just type 'http://www.mydomain.net/?sub2' and viola!
It goes to...
[sub2.mydomain.net...]
Thanks so much!
Do you have a solution where 'http://domain.com/?sub1' would be the same as 'http://domain.com?url=sub1'?
Also, I knew that it would work for other domains, I just said that it works for the hard-coded domain ;)
[edit]Glad you got it ;)[/edit]
I just wanted to know the coding and syntax to getting it done. There are several reasons to use this concept.
One reason is for time-saving.
For instance if I wanted to create redirect links for affiliate programs with links like the following, I could use this redirect file "on-the-fly" without having to create a separate redirect for each link.
In other words, those links that looks like this:
[domain1.com...]
[domain1.com...]
[domain1.com...]
I could create dynamically with the redirect file by...
[mydomain.com...]
xx being whatever number the link correspond to.
So with affiliate big affiliate networks, this will save mucho tiempo wouldn't you agree?
:)
I could create dynamically with the redirect file by...[mydomain.com...]
I'd use PATH_INFO instead of the query string. That would make the URLs much more search engine friendly and it's quite simple to code.