giarnz Posted August 1, 2007 Share Posted August 1, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/62947-php-redirect-code/ Share on other sites More sharing options...
teng84 Posted August 1, 2007 Share Posted August 1, 2007 $x=$_GET['x']; header("location: http://www.123domain.com/?ref=$x"); is that what you mean ????? Quote Link to comment https://forums.phpfreaks.com/topic/62947-php-redirect-code/#findComment-313424 Share on other sites More sharing options...
Philip Posted August 1, 2007 Share Posted August 1, 2007 <?php $ref = $_GET['ref']; $url = "http://www.blah.com/?id=".$ref; header("Location: $url"); ?> Is that what you are looking for? Quote Link to comment https://forums.phpfreaks.com/topic/62947-php-redirect-code/#findComment-313425 Share on other sites More sharing options...
teng84 Posted August 1, 2007 Share Posted August 1, 2007 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"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/62947-php-redirect-code/#findComment-313428 Share on other sites More sharing options...
Philip Posted August 1, 2007 Share Posted August 1, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/62947-php-redirect-code/#findComment-313436 Share on other sites More sharing options...
teng84 Posted August 1, 2007 Share Posted August 1, 2007 i know bro but who knows hes using that Quote Link to comment https://forums.phpfreaks.com/topic/62947-php-redirect-code/#findComment-313441 Share on other sites More sharing options...
Philip Posted August 1, 2007 Share Posted August 1, 2007 I figured you would, lol, I'm just throwing it out there, so we don't look like we forgot it Hopefully one of our suggestions solved their problem Quote Link to comment https://forums.phpfreaks.com/topic/62947-php-redirect-code/#findComment-313449 Share on other sites More sharing options...
giarnz Posted August 1, 2007 Author Share Posted August 1, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/62947-php-redirect-code/#findComment-313453 Share on other sites More sharing options...
giarnz Posted August 1, 2007 Author Share Posted August 1, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/62947-php-redirect-code/#findComment-313457 Share on other sites More sharing options...
Philip Posted August 2, 2007 Share Posted August 2, 2007 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? Quote Link to comment https://forums.phpfreaks.com/topic/62947-php-redirect-code/#findComment-313650 Share on other sites More sharing options...
giarnz Posted August 2, 2007 Author Share Posted August 2, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/62947-php-redirect-code/#findComment-313685 Share on other sites More sharing options...
Philip Posted August 2, 2007 Share Posted August 2, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/62947-php-redirect-code/#findComment-313688 Share on other sites More sharing options...
giarnz Posted August 2, 2007 Author Share Posted August 2, 2007 Hi KingPhilip, I have about 60 URLs in .htaccess though. Would this work for all of them? Also, what would the final .htaccess file look like? Thanks, Giarnz Quote Link to comment https://forums.phpfreaks.com/topic/62947-php-redirect-code/#findComment-313690 Share on other sites More sharing options...
Philip Posted August 2, 2007 Share Posted August 2, 2007 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? Quote Link to comment https://forums.phpfreaks.com/topic/62947-php-redirect-code/#findComment-313693 Share on other sites More sharing options...
giarnz Posted August 2, 2007 Author Share Posted August 2, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/62947-php-redirect-code/#findComment-313699 Share on other sites More sharing options...
Philip Posted August 2, 2007 Share Posted August 2, 2007 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? Quote Link to comment https://forums.phpfreaks.com/topic/62947-php-redirect-code/#findComment-313706 Share on other sites More sharing options...
giarnz Posted August 2, 2007 Author Share Posted August 2, 2007 Hi, That is the option that I thought I would have to use in the end. If I use a php script to handle it all, then I won't need to use .htaccess for it, since I can store the urls in the php script. Do you think that is a better option? Thanks, Giarnz Quote Link to comment https://forums.phpfreaks.com/topic/62947-php-redirect-code/#findComment-313708 Share on other sites More sharing options...
Philip Posted August 2, 2007 Share Posted August 2, 2007 Yeah, it probably be a lot easier to code. I'm no expert though, somebody else might know a really easy way to do it. I just don't have that knowledge Quote Link to comment https://forums.phpfreaks.com/topic/62947-php-redirect-code/#findComment-313710 Share on other sites More sharing options...
giarnz Posted August 2, 2007 Author Share Posted August 2, 2007 Hi, No worries. Thanks for your help and I'll poke around the forums. Giarnz Quote Link to comment https://forums.phpfreaks.com/topic/62947-php-redirect-code/#findComment-313715 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.