mod_rewrite, htaccess and Perl CGI

For the record, Rewrite rules do not like to be written as slashes in CGI-BIN URLs.

Perhaps this was known to others before, but was new to me. In your .htaccess file

Bad

RewriteRule ^(.*)$ /cgi-bin/books/$1 [QSA,L]

Good

RewriteRule ^(.*)$ /cgi-bin/books?$1 [QSA,L]

Notice the ? instead of the / in the good one.

For some reason, I was under the impression that using the slash, I could get the information in the URL using a PATHINFO e.g. [perl] my $stringtomangle = $cgi->pathinfo; [/perl]

The above would grab /page/main
from http://yourhost.com/cgi-bin/some.cgi/page/main

However, I now need to change this to [perl] my @bits = $cgi->keywords; my $stringtomangle = join ” “, @bits; [/perl] and this shows that I use the keywords method on the Perl CGI object.

This then would grab page/main
from http://yourhost.com/cgi-bin/some.cgi?page/main

You may want to combine this to be able to deal with either path_info or keywords. E.g. [perl] my $details = “”;

if ( (defined $cgi->pathinfo) and (length($cgi->pathinfo) ) { $details = $cgi->path_info; } elsif ( (defined $cgi->keywords) and (length($cgi->keywords) ) { my @bits = $cgi->keywords; $details = join ” “, @bits; } else { $details = “”; } [/perl]

Anyway .. a frustration now has an answer. I should probably have RTFM’ed !

Leave a Reply