jkushner jkushner - 1 year ago 54
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
we perform a redirect to the url without the
render=xml portion
. As you can see what im concerned with is the
query portion of the script.


i need to update the logic to perform the redirect and then i will need a substring based on the position of the
against the

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 Source

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;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download