geektron has asked for the wisdom of the Perl Monks concerning the following question:

ok, so it's more like PCRE than pure Perl, but at the crux of the issue is simplying URLs from a CGI::App-based script

i want to take:

www.mydomain.com/subDir/index.cgi?rm=foo - or - www.mydomain.com/subDir/index.cgi?rm=bar
and turn them into:
www.mydomain.com/subDir/foo www.mydomain.com/subDir/bar
and my ( paltry ) attempt at rewrite ( in .htaccess for subDir ):
RewriteEngine On + + RewriteCond %{REQUEST_URI} sale RewriteRule ^(.*) index.cgi?rm=$1 [L] RewriteRule ^(.*)\/ index.cgi?rm=$1 [L]
which only works without the trailing slash and is going to be unbearable to maintain if i have to put in lines for every run_mode ....

i know there's got to be a better way. i've read the rewriting guide but it's not clicking ....

Replies are listed 'Best First'.
Re: mod_rewrite woes
by The Mad Hatter (Priest) on Jan 29, 2004 at 23:21 UTC
    Here's what I think you want (in a .htaccess in /subDir/):
    RewriteEngine on RewriteBase /subDir/ # only redirect if the file requested isn't index.cgi # capture anything else and redirect it to index.cgi RewriteCond %{REQUEST_FILENAME} !^index.cgi RewriteRule ^(.+)/?$ index.cgi?rm=$1 [L]
    If you'd rather it be transparent to the user (that is, they don't see the index.cgi?rm=foo), then change the [L] to [P].
      thanks. i'll have to play with this. it fails with the same problem:
      No such run mode ( index.cgi ).
      i'll have to read up more later.
        FWIW, the example I posted works for me. Anyway, what that error hints to me is that the RewriteCond isn't working correctly (it's not letting requests to index.cgi through, instead it's redirecting them). If you turn on log level 9, mod_rewrite will write out *everything* it does; that should help in debugging.
Re: mod_rewrite woes
by The Mad Hatter (Priest) on Jan 29, 2004 at 20:44 UTC
    If you really want to do it with mod_rewrite, then read the doc about doing logical ORs in regular expressions (e.g. (foo|bar|baz|...)).

    Personally though, I would just treat the /foo and /bar as path info that a Perl script can then read and translate to the correct function. The extra path information is available through CGI.pm or CGI::Simple via the path_info method.

      i'm stuck w/ mod_rewrite. it's 'company protocol'.

      i'll hack on the RewriteCond.