in reply to redirecting, html forms, mechanize

Just out of curiosity - why aren't you using mod_rewrite for this? If you are using an Apache server (maybe you are not?), you can handle simple static redirections like the above directly in the server. Perl scripts are really only needed if you are dynamically selecting the redirection location or munging the post parameters before you redirect.

Best, beth

Replies are listed 'Best First'.
Re^2: redirecting, html forms, mechanize
by RobertCraven (Sexton) on Jun 28, 2009 at 01:02 UTC
    Dear Beth,

    thanks for your answer.
    I am not really a web-programmer or webmaster. I am just at the beginning to learn all the things Apache can do. I read a bit about the mod_rewrite, using it seems like a good idea. This task has been assigned pretty much last minute to me, is there a chance that you could give me some hints how to transfer files using mod_rewrite?

    Many thanks,
    Thomas

      Unfortunately, I can't give more than general help and learning tips. I'm only a casual user of mod_rewrite and don't have an experimental Apache server handy.

      On one foot, mod_rewrite lets you use Perl compatible regular expressions to convert a part of a URL you don't like to one you do like. The phrase "Perl compatible" should be taken with a small grain of salt - usually it means some subset of full-blown Perl regexes. I've never really done much more than simple rewrites, so I don't really know how full the emulation is (perhaps some other monk does?).

      The hardest part of mod_rewrite is learning its syntax for selecting URLs to be rewritten. In my experience, when mod_rewrite "doesn't work", it is usually a screw up with the selection process: thinking selection criteria are being or'd when they are really being and'd; regexes that don't do what you think they do; accidentally chaining together rules, and so on.

      Basically, you define a set of rules for selecting a URL using a series of RewriteCond statements. Each of these has three parts: the word "RewriteCond", a variable representing the thing you want to match (e.g. %{REMOTE_HOST}), and a regex to compare to it. These are the equivalent of $servervar =~ /.../ expressions in Perl.

      A series of these RewriteCond statements is normally and'd together but you can "or" them, by adding [OR] at the end of each RewriteCond statement. At the end of all of the RewriteCond statements, is a RewriteRule statement. This contains the URL transformation and has the format:

      RewriteRule regex-matching-what-you-want-to-get-rid-of text-you-actually-want.

      You can think of it as a fancy version of Perl substitution: s/regex-matching-what-you-want-to-get-rid-of/text-you-actually-want. So your redirection to MolSurfer might look something like this:

      RewriteCond %{REMOTE_HOST} ^host1.* [OR] RewriteCond %{REMOTE_HOST} ^host2.* [OR] RewriteCond %{REMOTE_HOST} ^host3.* RewriteRule regex replacement-for-regex [R,L]

      The things in square brackets at the end are instructions to the server. [OR] was already discussed above. L means "the end". If it is missing, and there is another set of RewriteCond/RewriteRule statements after it, Apache chains them, just as if you had successive $foo =~ s/blah/blech/ statements in a Perl script. [NC] means ignore case. See the documentation for other codes.

      The official documentation is very far from a tutorial, so here are some articles with examples. These should get you started. After reading them, you may want to go back to the mod_rewrite reference page and see if anything there starts making more sense.

      You might also try searching the net for Apache forums - you are likely to get more detailed help there.

      There is also a way you can insert a Perl script to do the URL conversion if you need to get a bit fancier with the conversion or use something like a lookup table stored in a database, but that is a lot more complex than you probabably need right now. If you are interested in it later on, scan the main mod_rewrite documentation page for the word "Perl". They have a short sample script.

      Best, beth

        Beth,

        thank you so much for your time and effort.
        After spending some time reading about mod_rewrite, I got the feeling that it is not really what I need. It seems mod_rewrite's intention is to handle complex redirections (and that it becomes really powerful in large networks). Too many bad experiences with "misusing" programs / servers for my needs made me stepping back using it.
        After doing more research about http, I realised what I really want is a redirect response back from my cgi.My script above was good, I only needed to change the _location to the requesting one in the http header.

        Although I did not use your suggestion, I learned a lot!
        Thank you!
        #!/usr/bin/perl use warnings; use strict; use Data::Dumper; use WWW::Mechanize; use CGI qw/:standard/; use CGI::Carp qw(warningsToBrowser fatalsToBrowser); my $q = new CGI; my $addr = $ENV{'REMOTE_ADDR'}; my $m = WWW::Mechanize->new(); my $url = 'http://projects.villa-bosch.de/dbase/molsurfer/submit-elec. +html'; $m->get($url); $m->add_header(location => $addr); $m->set_fields( FILE1 => $file1, FILE2 => $file2, mapsize =>'59x59', resolution =>'med', pdie =>'4', sdie =>'80', ioss =>'150', ionr =>'1.5', nmap =>'0.0' ); my $response = $m->submit(); print $q->header; print $response->content();