Help! - Need a CGI redirect which passes a querystring value

Discussion in 'Perl Misc' started by Damon, Jun 22, 2004.

  1. Damon

    Damon Guest

    Hi all,

    I'm not very familiar with Perl and could use some help.

    I need to have the page "myPage.cgi?cnt=60" redirect to
    "myPage.aspx?cnt=60". Can anyone give me an easy script for doing this
    redirect?

    Thanks for any help you can offer.

    -Damon
    Damon, Jun 22, 2004
    #1
    1. Advertising

  2. Damon

    Matt Garrish Guest

    "Damon" <> wrote in message
    news:...
    > Hi all,
    >
    > I'm not very familiar with Perl and could use some help.
    >
    > I need to have the page "myPage.cgi?cnt=60" redirect to
    > "myPage.aspx?cnt=60". Can anyone give me an easy script for doing this
    > redirect?
    >


    Assuming that is the only variable you're expecting, you could use the
    following in myPage.cgi:

    use strict;
    use warnings;
    use CGI;

    my $q = new CGI;

    my $cnt = $q->param('cnt');

    print $q->redirect("http://wherever/myPage.aspx?cnt=$cnt");


    Matt
    Matt Garrish, Jun 22, 2004
    #2
    1. Advertising

  3. Damon

    Damon Guest

    Thanks Matt,

    My only problem now is that this is all happening on a windows server.
    The code you provided doesn't seem to work in this environment. I just
    get the following error: "%1 is not a valid Win32 application."

    You see, we have several links hard-coded into our desktop application
    like "http://www.mydomain.com/myPage.cgi?cnt=50" which now need to
    point to "http://www.mydomain.com/myPage.aspx?cnt=50".

    Is there any way to make myPage.cgi?cnt=50 redirect and preserve the
    querystring on a windows server?

    -Damon

    "Matt Garrish" <> wrote in message news:<ExMBc.8742$>...
    > "Damon" <> wrote in message
    > news:...
    > > Hi all,
    > >
    > > I'm not very familiar with Perl and could use some help.
    > >
    > > I need to have the page "myPage.cgi?cnt=60" redirect to
    > > "myPage.aspx?cnt=60". Can anyone give me an easy script for doing this
    > > redirect?
    > >

    >
    > Assuming that is the only variable you're expecting, you could use the
    > following in myPage.cgi:
    >
    > use strict;
    > use warnings;
    > use CGI;
    >
    > my $q = new CGI;
    >
    > my $cnt = $q->param('cnt');
    >
    > print $q->redirect("http://wherever/myPage.aspx?cnt=$cnt");
    >
    >
    > Matt
    Damon, Jun 23, 2004
    #3
  4. Damon

    Matt Garrish Guest

    "Woody PurlGurl IV" <> wrote in message
    news:...
    > "Matt Garrish" <> wrote in message

    news:<ExMBc.8742$>...
    > > "Damon" <> wrote in message
    > > news:...
    > > > Hi all,
    > > >
    > > > I'm not very familiar with Perl and could use some help.
    > > >
    > > > I need to have the page "myPage.cgi?cnt=60" redirect to
    > > > "myPage.aspx?cnt=60". Can anyone give me an easy script for doing this
    > > > redirect?
    > > >

    > >
    > > Assuming that is the only variable you're expecting, you could use the
    > > following in myPage.cgi:
    > >
    > > use strict;
    > > use warnings;
    > > use CGI;

    >
    > Whoa! Isn't this _precisely_ the sort of situation where CGI.pm is
    > overkill? After all, all this takes is regex substitution on a cgi
    > environment variable:
    >
    > $ENV{REQUEST_URI} =~ s/myPage.cgi/myPage.aspx/;
    > $redirect = "http://" . $ENV{HTTP_HOST} . "/" . $ENV{REQUEST_URI};
    > print "Content-type: text/html\n\n";
    > print "Location: $redirect";
    >


    Uh, why are you sending an html content-type header? You do realize that
    you're just going to print the new url to the browser, right?

    More to the point, though, you're assuming that those variables are
    available. In his follow-up post he mentions he's on a Windows server, and
    IIS does not recognize REQUEST_URI. Another reason to push people in the
    direction of CGI.pm over hand-rolled "solutions"... : )

    Matt
    Matt Garrish, Jun 23, 2004
    #4
  5. Damon wrote:
    > Is there any way to make myPage.cgi?cnt=50 redirect and preserve the
    > querystring on a windows server?


    I don't understand why Matt's suggestion would not work on Windows.
    Anyway, as long as the key and value only consist of ASCII characters,
    this should be sufficient:

    print 'Location: http://www.mydomain.com/myPage.aspx?',
    "$ENV{QUERY_STRING}\n\n";

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, Jun 23, 2004
    #5
  6. Damon

    Matt Garrish Guest

    "Damon" <> wrote in message
    news:...
    > Thanks Matt,
    >
    > My only problem now is that this is all happening on a windows server.
    > The code you provided doesn't seem to work in this environment. I just
    > get the following error: "%1 is not a valid Win32 application."
    >
    > You see, we have several links hard-coded into our desktop application
    > like "http://www.mydomain.com/myPage.cgi?cnt=50" which now need to
    > point to "http://www.mydomain.com/myPage.aspx?cnt=50".
    >
    > Is there any way to make myPage.cgi?cnt=50 redirect and preserve the
    > querystring on a windows server?
    >


    <please don't top-post>

    The script I provided should work regardless of what value of cnt is passed
    to it. The error you note sounds like it's in the way that you have the .cgi
    extension mapped. Do any .cgi scripts work on your server?

    Matt
    Matt Garrish, Jun 23, 2004
    #6
  7. Damon

    John Bokma Guest

    Woody PurlGurl IV wrote:

    > "Matt Garrish" <> wrote in message news:<ExMBc.8742$>...
    >
    >>"Damon" <> wrote in message
    >>news:...
    >>
    >>>Hi all,
    >>>
    >>>I'm not very familiar with Perl and could use some help.
    >>>
    >>>I need to have the page "myPage.cgi?cnt=60" redirect to
    >>>"myPage.aspx?cnt=60". Can anyone give me an easy script for doing this
    >>>redirect?
    >>>

    >>
    >>Assuming that is the only variable you're expecting, you could use the
    >>following in myPage.cgi:
    >>
    >>use strict;
    >>use warnings;
    >>use CGI;

    >
    >
    > Whoa! Isn't this _precisely_ the sort of situation where CGI.pm is
    > overkill? After all, all this takes is regex substitution on a cgi
    > environment variable:
    >
    > $ENV{REQUEST_URI} =~ s/myPage.cgi/myPage.aspx/;


    Um... if my URI is eh... myP%61ge.cgi?

    and why are you rewriting myPagescgi to myPage.aspx?

    --
    John MexIT: http://johnbokma.com/mexit/
    personal page: http://johnbokma.com/
    Experienced Perl programmer available: http://castleamber.com/
    Happy Customers: http://castleamber.com/testimonials.html
    John Bokma, Jun 23, 2004
    #7
  8. Damon

    raymond Guest

    Should not be sent content-type for redirection so need this line for below example

    print "Content-type: text/html\n\n"; **no need


    raymond raj

    (Woody PurlGurl IV) wrote in message news:<>...
    > "Matt Garrish" <> wrote in message news:<ExMBc.8742$>...
    > > "Damon" <> wrote in message
    > > news:...
    > > > Hi all,
    > > >
    > > > I'm not very familiar with Perl and could use some help.
    > > >
    > > > I need to have the page "myPage.cgi?cnt=60" redirect to
    > > > "myPage.aspx?cnt=60". Can anyone give me an easy script for doing this
    > > > redirect?
    > > >

    > >
    > > Assuming that is the only variable you're expecting, you could use the
    > > following in myPage.cgi:
    > >
    > > use strict;
    > > use warnings;
    > > use CGI;

    >
    > Whoa! Isn't this _precisely_ the sort of situation where CGI.pm is
    > overkill? After all, all this takes is regex substitution on a cgi
    > environment variable:
    >
    > $ENV{REQUEST_URI} =~ s/myPage.cgi/myPage.aspx/;
    > $redirect = "http://" . $ENV{HTTP_HOST} . "/" . $ENV{REQUEST_URI};
    > print "Content-type: text/html\n\n";
    > print "Location: $redirect";
    >
    > PurlGurl, do you have a woody for me now too? WooHoo!
    raymond, Jun 23, 2004
    #8
  9. Damon

    John Bokma Guest

    John Bokma, Jun 23, 2004
    #9
  10. On Wed, 22 Jun 2004, raymond blurted out atop a fullquote:

    > Should not be sent content-type for redirection


    Wrong. RTFRFC. This is off-topic for a Perl language group so I'll
    say no more about that. Discerning usenauts know that anything posted
    in TOFU format needs to be distrusted.
    Alan J. Flavell, Jun 23, 2004
    #10
  11. Damon

    Joe Smith Guest

    Woody PurlGurl IV wrote:

    > $ENV{REQUEST_URI} =~ s/myPage.cgi/myPage.aspx/;
    > $redirect = "http://" . $ENV{HTTP_HOST} . "/" . $ENV{REQUEST_URI};
    > print "Content-type: text/html\n\n";
    > print "Location: $redirect";


    You've got the newlines in the wrong location.

    $ENV{REQUEST_URI} =~ s/myPage\.cgi/myPage.aspx/;
    $redirect = "http://" . $ENV{HTTP_HOST} . "/" . $ENV{REQUEST_URI};
    print <<EOM;
    Location: $redirect
    Content-type: text/html

    <html><body>Your browser should have automatically gone to
    <a href="$redirect">$redirect</a></body></html>
    EOM

    -Joe
    Joe Smith, Jun 23, 2004
    #11
  12. "Matt Garrish" <> wrote in message news:<754Cc.25916$>...
    > "Woody PurlGurl IV" <> wrote in message
    > news:...
    > > "Matt Garrish" <> wrote in message

    > news:<ExMBc.8742$>...
    > > > "Damon" <> wrote in message
    > > > news:...
    > > > > Hi all,
    > > > >
    > > > > I'm not very familiar with Perl and could use some help.
    > > > >
    > > > > I need to have the page "myPage.cgi?cnt=60" redirect to
    > > > > "myPage.aspx?cnt=60". Can anyone give me an easy script for doing this
    > > > > redirect?
    > > > >
    > > >
    > > > Assuming that is the only variable you're expecting, you could use the
    > > > following in myPage.cgi:
    > > >
    > > > use strict;
    > > > use warnings;
    > > > use CGI;

    > >
    > > Whoa! Isn't this _precisely_ the sort of situation where CGI.pm is
    > > overkill? After all, all this takes is regex substitution on a cgi
    > > environment variable:
    > >
    > > $ENV{REQUEST_URI} =~ s/myPage.cgi/myPage.aspx/;
    > > $redirect = "http://" . $ENV{HTTP_HOST} . "/" . $ENV{REQUEST_URI};
    > > print "Content-type: text/html\n\n";
    > > print "Location: $redirect";
    > >

    >
    > Uh, why are you sending an html content-type header? You do realize that
    > you're just going to print the new url to the browser, right?


    Just a little cargo cult code to make you feel at home. Let's see,
    what's worse, one needless line, or including a 100K library
    needlessly?

    > More to the point, though, you're assuming that those variables are
    > available. In his follow-up post he mentions he's on a Windows server, and
    > IIS does not recognize REQUEST_URI. Another reason to push people in the
    > direction of CGI.pm over hand-rolled "solutions"... : )


    Baloney. Trust me, there are cgi-environment variables on Windows
    that should do the trick. Knowing them and/or knowing how to find
    them is a _good_thing_.

    Although others beside you have correctly criticized my code in parts,
    _nobody_ else has defended your needless use of CGI.pm. In particular
    I like Gunnar's solution:

    print 'Location: http://www.mydomain.com/myPage.aspx?',
    "$ENV{QUERY_STRING}\n\n";

    Note the similarity of concept to my conception. The "ayes" have it
    ;)
    Woody PurlGurl IV, Jun 23, 2004
    #12
  13. Damon

    Matt Garrish Guest

    "Woody PurlGurl IV" <> wrote in message
    news:...
    >
    > > More to the point, though, you're assuming that those variables are
    > > available. In his follow-up post he mentions he's on a Windows server,

    and
    > > IIS does not recognize REQUEST_URI. Another reason to push people in the
    > > direction of CGI.pm over hand-rolled "solutions"... : )

    >
    > Baloney. Trust me, there are cgi-environment variables on Windows
    > that should do the trick. Knowing them and/or knowing how to find
    > them is a _good_thing_.
    >
    > Although others beside you have correctly criticized my code in parts,
    > _nobody_ else has defended your needless use of CGI.pm. In particular
    > I like Gunnar's solution:
    >


    Why would anyone post a defense of a working solution? And why provide
    someone a non-working and non-portable solution as you attempted to do? I
    see your alter-ego is just as out-to-lunch as the original.

    Matt
    Matt Garrish, Jun 23, 2004
    #13
  14. On Wed, 23 Jun 2004, Matt Garrish revealed to all and sundry that:

    > "Woody PurlGurl IV" <> wrote in message


    > > _nobody_ else has defended your needless use of CGI.pm.


    Try reading this group for a while. Lurk before you leap.

    > Why would anyone post a defense of a working solution? And why provide
    > someone a non-working and non-portable solution as you attempted to do? I
    > see your alter-ego is just as out-to-lunch as the original.


    Looks that way to me, too.

    As for the history of Gunnar recommending solutions which bypass
    CGI.pm, the relevant discussions from past exchanges can be consulted
    at the usual news archives. I don't see any benefit in going over
    them yet again.
    Alan J. Flavell, Jun 23, 2004
    #14
  15. Alan J. Flavell wrote:
    > As for the history of Gunnar recommending solutions which bypass
    > CGI.pm, the relevant discussions from past exchanges can be
    > consulted at the usual news archives.


    Even if I disagree on your using of the word "bypass", I won't argue
    this time.

    > I don't see any benefit in going over them yet again.


    That's fair. So, why did you refer to them in the first place? If you
    disapprove of the suggestion I posted in this thread, wouldn't it have
    been more suitable to say so?

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, Jun 23, 2004
    #15
  16. On Wed, 23 Jun 2004, Gunnar Hjalmarsson wrote:

    > > I don't see any benefit in going over them yet again.

    >
    > That's fair. So, why did you refer to them in the first place?


    To encourage anyone who's seriously interested to go and check a news
    archive, if they haven't already done so.

    > If you disapprove of the suggestion


    My views are more complex than that, as I thought I had made clear
    before. But I don't want to get involved in troll-feeding again:
    you are sufficiently acquainted with the climate around here to work
    out for yourself whether you want to get dragged-in or not, and I've
    really said all that I thought it worthwhile to say.

    good luck.

    --
    I'm afraid you have fallen into a well-known trap of understanding
    what is going on here. -Phil Hazel on exim-users
    Alan J. Flavell, Jun 23, 2004
    #16
  17. Damon

    Damon Guest

    "Matt Garrish" <> wrote in message news:<fb4Cc.25944$>...
    > "Damon" <> wrote in message
    > news:...
    > > Thanks Matt,
    > >
    > > My only problem now is that this is all happening on a windows server.
    > > The code you provided doesn't seem to work in this environment. I just
    > > get the following error: "%1 is not a valid Win32 application."
    > >
    > > You see, we have several links hard-coded into our desktop application
    > > like "http://www.mydomain.com/myPage.cgi?cnt=50" which now need to
    > > point to "http://www.mydomain.com/myPage.aspx?cnt=50".
    > >
    > > Is there any way to make myPage.cgi?cnt=50 redirect and preserve the
    > > querystring on a windows server?
    > >

    >
    > <please don't top-post>
    >
    > The script I provided should work regardless of what value of cnt is passed
    > to it. The error you note sounds like it's in the way that you have the .cgi
    > extension mapped. Do any .cgi scripts work on your server?
    >
    > Matt


    Hi all,

    Sorry about the top-posting.

    Thanks to all for your help. Both Matt and Gunnar's solutions solved
    my problem. I found out that the Windows server I am hosted on was not
    correctly setup to process CGI. Once this was fixed, both scripts
    worked.

    Thanks for all your help! I really appreciate it...

    -Damon
    Damon, Jun 23, 2004
    #17
  18. "Matt Garrish" <> wrote in message
    >
    > Why would anyone post a defense of a working solution? And why provide
    > someone a non-working and non-portable solution as you attempted to do?


    Its called a "trade-off". Your approach may be portable, but for this
    one-off script the performance cost of using CGI.pm is unnecessary,
    and depending on volume, perhaps even prohibitive. And certainly
    avoidable.

    Discussions in this group occasionally concede that using CGI.pm may
    not always be necessary. This in my opinion is a perfect scenario.
    Why load literally 1000 times more code than necessary (100 K vs 100
    bytes, give or take)?

    > I see your alter-ego is just as out-to-lunch as the original.


    A) Of course this must be PurlGurl's alter ego. After all, PurlGurl
    is the only person on planet earth that ever uses the phrase "cargo
    cult". Oops, except for the compilers of foldoc, apparently.

    B) You are the one who is "out to lunch" if you are not willing to
    concede even the _possibility_ of a valid trade-off in this scenario.
    Woody PurlGurl IV, Jun 24, 2004
    #18
  19. Damon

    Matt Garrish Guest

    "Woody PurlGurl IV" <> wrote in message
    news:...
    > "Matt Garrish" <> wrote in message
    > >
    > > Why would anyone post a defense of a working solution? And why provide
    > > someone a non-working and non-portable solution as you attempted to do?

    >
    > Its called a "trade-off". Your approach may be portable, but for this
    > one-off script the performance cost of using CGI.pm is unnecessary,
    > and depending on volume, perhaps even prohibitive. And certainly
    > avoidable.
    >


    Based on what fact? Both mod_perl and aspx scripts only need to compile once
    (and with mod_perl you can precompile the modules). You need to find a
    better argument.

    >
    > Discussions in this group occasionally concede that using CGI.pm may
    > not always be necessary. This in my opinion is a perfect scenario.
    > Why load literally 1000 times more code than necessary (100 K vs 100
    > bytes, give or take)?
    >


    I never said it was the only way. You jumped at the chance to trash CGI.pm
    and posted some garbage. I pointed out you were wrong. I never said anything
    to the people who posted workable solutions.

    Matt
    Matt Garrish, Jun 24, 2004
    #19
  20. Matt Garrish wrote:
    > Both mod_perl and aspx scripts only need to compile once (and with
    > mod_perl you can precompile the modules).


    Don't take for granted that mod_perl is an available option. The OP in
    this thread used the wording "the Windows server I am hosted on".

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, Jun 24, 2004
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mehdi
    Replies:
    6
    Views:
    36,103
    sloan
    Apr 6, 2006
  2. Terry Jones
    Replies:
    3
    Views:
    792
    Nick Kew
    Mar 24, 2005
  3. Timo
    Replies:
    6
    Views:
    568
    Alexey Smirnov
    Jan 14, 2008
  4. Replies:
    2
    Views:
    412
    Howard Hinnant
    Jan 8, 2009
  5. Sal
    Replies:
    1
    Views:
    406
Loading...

Share This Page