Author image Thomas Klausner
and 1 contributors


Apache::URI2param - PerlInitHandler to use with CGI::URI2param


in your httpd.conf

  <Location /somewhere>
     PerlInitHandler Apache::URI2param

     PerlAddVar URI2param_regex "sort sort_(\w+)"
     PerlAddVar URI2param_regex "style style_(fancy|plain)"
     PerlAddVar URI2param_regex "id news(\d+)"

  <Location /somewhere/else>
     PerlAddVar URI2param_regex "id article(\d+)"


Apache::URI2param is a small PerlInitHandler to wrap around CGI::URI2param so you don't have to call CGI::URI2param::uri2param from your mod_perl scripts/apps.

As an added bonus, it uses PerlAddVar to set the regexes, so you can let Apache figure out what regexes to apply to what URIs via the Apache Configuration File.

You should start your own handlers with: sub handler { my $r=Apache::Request->instance(shift); ...

i.e., use the new feature of Apache::request, instance to use a singelton Apache Request object.


After installing Apache::URI2param as a PerlInitHandler, you can pass regexes that should be applied to the URI via PerlAddVar. The format is:

  PerlAddVar URI2param_regex "PARAM REGEX"

where PARAM is the name of the parameter to be set, and REGEX is a regular expression containing capturing parenthenses.

You should use PerlAddVar instead of PerlSetVar, because Apache will then figure out for you what regexes to apply for an given URI.

If you look at the example given in SYNOPSIS, if you'd request the URI /somewhere/else/style_fancy/article123.html you would get the following parameters:

    print $r->param('style') # fancy
    print $r->param('id')    # 123
    print $r->param('sort')  # undef

As you can see here, you can use the "style" regex defined for /somewhere, but the "id" definition in /somewhere/else overrides the one in /somewhere.


This routine gets called as a PerlInitHandler very early in the Apache Request Cycle. Thus you can access the generated params in nearly all other phases.

handler basically just generates a hash of param names and regexes and passes the hash to CGI::URI2param::uri2param for processing there.


Apache::URI2param gets installed along with CGI::URI2param


Apache::Request (0.33)




Thomas Klausner,,

Thanks Darren Chamberlain <> for the idea to write a mod_perl handler for CGI::URI2param


Copyright 2002, 2006 Thomas Klausner, All Rights Reserved.

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.