jkushner jkushner - 1 month ago 7
Perl Question

Redirect based on query

I have the following code:

my $uri=$ENV{'REQUEST_URI'};
if ($uri=~/\/\?render=xml){
redirect(301,"Moved Permanently","urlsubstring");
}


What I'm trying to accomplish is if a user goes anywhere on my site if the page looks like
http://wwww.something.com/about/?render=xml
we perform a redirect to the url without the
render=xml portion
. As you can see what im concerned with is the
render=xml
query portion of the script.

So,

i need to update the logic to perform the redirect and then i will need a substring based on the position of the
?render=xml
against the
url
.

Here's my substring code:

$pos = index($uri,"?render=xml");
$newuri = substr($uri,0,$pos);


I'm very new to perl so i just am looking for some assistanvce

Answer

firstly, you have not terminated your search pattern.

my $uri=$ENV{'REQUEST_URI'};
if ($uri=~/\/\?render=xml/){
 redirect(301,"Moved Permanently","urlsubstring");
}

but you can just do a straight search and replace. try this.

    $uri = "http://www.something.com/about/?render=xml";
    $uri =~ s/\?render\=xml/newurlstring/g;
    print $uri

Untested, but you would more than likely have something like this as your code.

my $uri=$ENV{'REQUEST_URI'};
$uri =~ s/\?render\=xml/newurlstring/g;

which will return $uri as http://www.something.com/about/newurlstring

or if you just want to dump the ?render=xml portion and only use $uri as http://www.something.com/about/

my $uri=$ENV{'REQUEST_URI'};
$uri =~ s/\?render\=xml//g;
Comments