Forum Moderators: coopster

Message Too Old, No Replies

Dynamic PHP Redirect

How to do PHP redirect based on query string?

         

nguyen

4:15 am on Jun 29, 2006 (gmt 0)

10+ Year Member



Greetings!

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...

[directory1.mydomain.com...]

Any help is appreciated.

Thanks!
Nguyen

eelixduppy

4:21 am on Jun 29, 2006 (gmt 0)



Try something like this:

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!

vincevincevince

4:22 am on Jun 29, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Use the content of $_SERVER['QUERY_STRING']

nguyen

4:51 am on Jun 29, 2006 (gmt 0)

10+ Year Member



eelixduppy:

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");
?>

Hope I'm explaining this clear...

eelixduppy

4:58 am on Jun 29, 2006 (gmt 0)



You can do it like this, assuming you want both types of redirects within the same script:

$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!

nguyen

5:05 am on Jun 29, 2006 (gmt 0)

10+ Year Member



eelixduppy,

Hey, that looks good. Let me go try that out real quick.

nguyen

5:14 am on Jun 29, 2006 (gmt 0)

10+ Year Member



Hmmm...this would only redirect within the domain of the redirect file only right?

Would it redirect to a different domain? Like to [subdomain.yourdomain.com?...]

nguyen

5:26 am on Jun 29, 2006 (gmt 0)

10+ Year Member



eelixduppy,

Works great. Thanks a lot!

Now if only I can get rid of the '?url=' and just enter '?subdir' without the 'url='....

Any ideas?

eelixduppy

5:31 am on Jun 29, 2006 (gmt 0)



Yes, it's only within the hard-coded domain. Now that I think about it, I would just go with this solution:

$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!

nguyen

5:43 am on Jun 29, 2006 (gmt 0)

10+ Year Member



eelixduppy,

Actually, your previous suggestion does exactly what I was looking for.

I'm learning quite a bit from you today.

Do you have a solution where 'http://domain.com/?sub1' would be the same as 'http://domain.com?url=sub1'?

Muchas Gracias!

nguyen

5:44 am on Jun 29, 2006 (gmt 0)

10+ Year Member



By the way,

It does redirect to other domains besides the domain the redirect file is hosted.

nguyen

5:50 am on Jun 29, 2006 (gmt 0)

10+ Year Member



eelixduppy,

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!

eelixduppy

5:52 am on Jun 29, 2006 (gmt 0)




Do you have a solution where 'http://domain.com/?sub1' would be the same as 'http://domain.com?url=sub1'?

I'm not quite understanding your purpose for this. Why do you want this?

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]

nguyen

6:14 am on Jun 29, 2006 (gmt 0)

10+ Year Member



:)

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?

:)

eeek

8:07 pm on Jun 30, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



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.