Click to See Complete Forum and Search --> : Redirect based on URL


ck_net_2004
04-24-2011, 02:38 AM
First up I know next to nothing about Perl, im a PHP person when it comes to webdevelopment. So i'm hoping someone here is willing to help me with this basic script.

Basicly I want to redirect the person to the new system based on the page ID of the item they went to.

I'm used to GET variables, but this old system was a tad different as it had the ID number as the last part of the URL without any ? beforehand.

an example of such a URL:
cgi-bin/cms/Item/01234

I went and found a basic redirection script from the internet, and what I want to do first is modify it to get the full URL of the script (using the environment variable SCRIPT_PATH perhaps?), split it up by the forward slashes, and if the seccond-last one equals "Item", then the last one will equal the ID i'm after.

Once I have the ID from the old system, I need to reference a hashtable/array to see what the new ID is on the new system. Again I have no idea how to write a hashtable in Perl.

Once I have those two things I can just redirect to the new page.

Heres the redirect I grabbed off the internet:

#!/usr/bin/perl
#
# fixedredir.cgi

use strict;
use warnings;

my $URL = "http://www.example.com/";

print "Status: 302 Moved\nLocation: $URL\n\n";


I feel guilty asking for someone to code it completely for me, but I really don't know Perl at all, and im sure for someone who knows it inside out this would take 30secconds.

Cheers for any help/links people can provide.

Regards Chris

ck_net_2004
04-25-2011, 06:46 AM
Heres what ive put together so far, I have no way of testing this offline, and im sure this must have all sorts of errors at the moment. So I hope someone can fix this up:

@urlPieces is supposed to be the full page URL split by forwardslash.
$l is supposed to be how many indexes there is in the @urlPieces array.
%ids is supposed to be a hashtable of old Ids to allow me to take the Id in the URL, and find the new one.


#!/usr/bin/perl
#
# fixedredir.cgi

use strict;
use warnings;

my $RedirectTo = "http://www.example.com/";

@urlPieces = split(/\//, abs_path($0));

$l = scalar(@urlPieces);

if(@urlPieces[$l - 2] eq "Item")
{
%ids = (
0123 => "id1",
0124 => "id2",
0523 => "id3");

$newID = $ids{@urlPieces[$l - 1]};
$RedirectTo .= "newPage.php?id=$newID";
}

print "Status: 302 Moved\nLocation: $RedirectTo\n\n";

Sixtease
04-26-2011, 03:57 AM
Hey there, googling for "CGI URI variable" led me to believe that the path is in $ENV{PATH_INFO}, so I'd do about the following:

my @url_pieces = split /\//, $ENV{PATH_INFO};
my $id = pop @url_pieces;
my $item = pop @url_pieces;
if ($item eq 'item') {
...
}

Otherwise your code looks quite sane. HTH

ck_net_2004
04-28-2011, 05:04 AM
Hey there, googling for "CGI URI variable" led me to believe that the path is in $ENV{PATH_INFO}, so I'd do about the following:

my @url_pieces = split /\//, $ENV{PATH_INFO};
my $id = pop @url_pieces;
my $item = pop @url_pieces;
if ($item eq 'item') {
...
}

Otherwise your code looks quite sane. HTH


Thanks for your reply, is there any way to check the key is within the hashtable? Just to prevent an error if id within the URL is not within the hashtable.
Cheers Chris

Sixtease
04-28-2011, 05:10 AM
if (exists $ids{$id}) { ... }
or negated
if (not exists $ids{$id} { ... }

ck_net_2004
04-28-2011, 06:25 AM
if (exists $ids{$id}) { ... }
or negated
if (not exists $ids{$id} { ... }

Brilliant, I think i'm set now!

Thanks so much for your help, I felt rather lost with Perls syntax

Best regards Chris