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

Dear Monks,

My perl CGI URLs have the following form:
http://www.mywebsite.com/user.cgi?user=jack
I need to rewrite the URL to be something like:
http://www.mywebsite.com/users/jack
Or I have to make it more user-friendly in one way or another.

Could someone please point me to a useful Perl or Apache Module for making this? Or maybe someone is using their own technique of handling this?

Thank you very much

Replies are listed 'Best First'.
Re: URL rewriting
by valdez (Monsignor) on Apr 01, 2004 at 13:16 UTC

    You can use mod_rewrite as suggested by maa; see also URL Rewriting Guide. If you want to use mod_perl, see Using mod_perl to rewrite URLs. If you want to roll your own, you can do something like this inside your user.cgi:

    #!/usr/bin/perl use CGI; use URI; $q = CGI->new; $user = $q->param('user'); $uri = URI->new; $uri->scheme('http'); $uri->host('www.mywebsite.com'); $uri->path("/users/$user"); print $q->redirect( -uri => $uri->as_string ); exit;

    Ciao, Valerio

Re: URL rewriting
by maa (Pilgrim) on Apr 01, 2004 at 09:03 UTC
Re: URL rewriting
by saintbrie (Scribe) on Apr 02, 2004 at 04:03 UTC
    Since I can assume you are using apache, I know this will work: If "/user" is executable, when the browser calls /user, /jack will be in the PATH_INFO environment variable, which you can use raw $ENV{PATH_INFO} or you can get at using CGI.pm (or other modules):
    use CGI; $C = CGI->new(); $temp_user = $C->path_info(); $user = ""; # for taint checking if ($temp_user =~ /^\/(\w+)/) { $user = $1; } # do something with $user here # which if called as /user/jack will be "jack"