Jump to content

Recommended Posts

Hi all,

I am bogged on some PHP coding and I am hoping someone here could help me please.

 

I am trying to redirect a link using PHP and strip/extract certain parameters from the URL when redirecting.

 

 

 

For example, my url is http://www.123domain.com/?ref=12345 and I am trying to redirect the domain but KEEP the 12345 part. I am using the code to extract the 12345 part:

 

<?php

$ref = $_GET['ref'];

$ref = urlencode($ref);

?>

 

Now, the above code works perfectly on the same page, meaning, if I put <?php echo $ref; ?> on the page, it will display "12345". What I am trying to do though is redirect and keep the 12345 in the redirected URL.

 

For example, I am trying to redirect http://www.123domain.com/?ref=12345 to http://www.456domain.com/?id=12345 using another php or redirect file, and NOT linking directly to it.

 

Could someone please tell me how to grab the "12345" section from the current URL or (referred URL) and add this to the redirected URL?

 

 

Thanks,

 

Giarnz

Link to comment
https://forums.phpfreaks.com/topic/62947-php-redirect-code/
Share on other sites

BTW we both made a mistake

 

look

<?php
$ref = $_GET['ref'];
$url = "http://www.blah.com/?id=".$ref;
header("Location: $url");
?>

should be

<?php
$ref = $_GET['ref'];
$url = "http://www.blah.com/thepagehere.php?id=".$ref;
header("Location: $url");
?>

 

Link to comment
https://forums.phpfreaks.com/topic/62947-php-redirect-code/#findComment-313428
Share on other sites

Not necessarily, .htaccess can be used to clean up URLs, like blah.com/index.php?action=home&user=phil to blah.com/home/phil/

 

It is possible that it is http://www.blah.com/index.php covered up to only show http://www.blah.com/

 

.htaccess can do wonders with the mod rewrite ;)

Link to comment
https://forums.phpfreaks.com/topic/62947-php-redirect-code/#findComment-313436
Share on other sites

Hi all,

Thanks teng84 and KingPhilip  for your help. I tried

<?php

$ref = $_GET['ref'];

$url = "http://www.blah.com/thepagehere.php?id=".$ref;

header("Location: $url");

?>

 

and it is returning an error for me.

 

 

What I am trying to do is this:

 

My URL ---> http://www.123domain.com/?ref=12345

 

Redirect File ---> http://www.123domain.com/456.php.php

 

Destination URL ---> http://www.456domain.com?id=12345&url=http%3A%2F%2Fwww.789domain.com%2F

 

 

So, instead of linking directly to the "Destination URL", I want to first go to the "Redirect File" and then the "Destination URL". This way, I can easily modify a single file instead of updating multiple pages each time when I have to modify a destination.

 

 

I also tried using .htacess and it works for redirecting, but I can't seem to add the ?ref=12345 and convert it to id=12345.

 

Using .htaccess, I tried the following:

/456.php "http://www.456domain.com?id=<?php echo $keywordget; ?>&url=http%3A%2F%2Fwww.789domain.com%2F"

 

 

Thanks again,

 

Giarnz

 

Link to comment
https://forums.phpfreaks.com/topic/62947-php-redirect-code/#findComment-313453
Share on other sites

Also, if I could use .htaccess to do it all I would love to as then I only need 1 file for everything and the redirect files are all aliases because the .htaccess would redirect them immediately.

 

I just don't know if I can dynamically alter the URL with .htaccess to include the ref/id information.

Link to comment
https://forums.phpfreaks.com/topic/62947-php-redirect-code/#findComment-313457
Share on other sites

I don't know how much about .htacces you know, but here's a shot for ya:

 

Options +FollowSymlinks
RewriteEngine on
RewriteRule ^?ref=([0-9]+)$ 456.php?refID=$1 [R]

 

That would get them to the 2nd page (redirect file). Did you just want to do it in one swoop to get them to the 456domain.com, or still go through the redirect file?

 

Link to comment
https://forums.phpfreaks.com/topic/62947-php-redirect-code/#findComment-313650
Share on other sites

Hi KingPhilip,

I would like to do it all in 1 swoop and miss the redirect file if possible, but I need to copy the REF data from the current URL and then add it to the URL in .htaccess. That's why I tried to add PHP code .htaccess but I now know that you can't add PHP code to .htaccess files.

 

I am under the impression that I need a PHP script to do this for me.

 

 

Thanks,

 

Giarnz

Link to comment
https://forums.phpfreaks.com/topic/62947-php-redirect-code/#findComment-313685
Share on other sites

You can copy the REF data from the current URL and then add it into the URL

 

It's shown in my above post

RewriteRule ^?ref=([0-9]+)$ 456.php?refID=$1 [R]

Basically it says:

Rewrite the URL that has ?ref=(any numerical value, lets call it X) and throw the user to 456.php?refID=X

Link to comment
https://forums.phpfreaks.com/topic/62947-php-redirect-code/#findComment-313688
Share on other sites

Wow, well, it depends on what the URLs are for.

 

If they are all for the same rewrite, where you are redirecting the user to the same page, then you could just use this code:

Options +FollowSymlinks
RewriteEngine on
RewriteRule ^?ref=([0-9]+)$ 456.php?refID=$1 [R]

 

Could you explaina little more indepth, or give a few examples of what you already have?

Link to comment
https://forums.phpfreaks.com/topic/62947-php-redirect-code/#findComment-313693
Share on other sites

Hi,

I have pasted a few entried from .htaccess:

Redirect /company.php http://www.blabla.com/abcdefg?ref=<REF GOES HERE>&url=http%3A%2F%2Fwww.456domain.com%2Fabout%2F

Redirect /communities.php http://www.blabla.com/abcdefg?ref=<REF GOES HERE>&url=http%3A%2F%2Fwww.456domain.com%2Fcommunities%2F

Redirect /downloads.php http://www.blabla.com/abcdefg?ref=<REF GOES HERE>&url=http%3A%2F%2Fwww.456domain.com%2Fdownloads%2F

Redirect /upgrade.php http://www.blabla.com/abcdefg?ref=<REF GOES HERE>&url=http%3A%2F%2Fwww.456domain.com%2Fupgrade%2F

 

So, if a user clicks on a link which points to www.123domain.com/company.php, it should redirect them to http://www.blabla.com/abcdefg?ref=<REF GOES HERE>&url=http%3A%2F%2Fwww.456domain.com%2Fabout%2F and replace <REF GOES HERE> with the REF data from the current URL.

 

So, if the user is on www.123domain.com/?ref=g-34-343-23 and they click on www.123domain.com/company.php, they should be redirected to http://www.blabla.com/abcdefg?ref=g-34-343-23&url=http%3A%2F%2Fwww.456domain.com%2Fabout%2F

 

 

Thanks,

 

Giarnz

Link to comment
https://forums.phpfreaks.com/topic/62947-php-redirect-code/#findComment-313699
Share on other sites

Okay, this is a bit more complicated than I was hoping for.

 

One way that I can think of doing this, its on the pages have the link with the ref code on the end, so the link would be like (dynamically with PHP) www.123domain.com/company.php?ref=12345 (in php terms: echo "www.123domain.com/company.php?ref=".$GET['ref'];)

 

That would make things a lot easier, would that be possible?

Link to comment
https://forums.phpfreaks.com/topic/62947-php-redirect-code/#findComment-313706
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.