Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

mod_rewrite + CGI.pm in "reverse"?

by hacker (Priest)
on Mar 11, 2005 at 15:22 UTC ( [id://438673]=perlquestion: print w/replies, xml ) Need Help??

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

I've written a new mod_perl-driven application which takes a URI (with the scheme of news://, http://, and ftp:// so far), a limit, and some other params, and passes them into my script.

CGI.pm splits off param(), validates the input, and passes the params off to the proper subs, based on the values of the data found. So far, so good...

What I'm trying to figure out, is a way to take the following style URI fragment, which includes the "raw" params and query string:

http://www.example.com/?type=news&feed=news://comp.lang.c&limit=10

...and turn it into something "nicer" for the user to remember and use in the future, like this:

http://www.example.com/news/comp.lang.c/10

I'm not the best with mod_rewrite, and I think it can help here, but I'm not sure how to "split" the fragments passed in on '/', and pass those to CGI.pm to parameterize.

Would something like this help?

RewriteRule ^/(.*)/(.*)/(.*)   /index.pl?type=$1&feed=$2&limit=$3

Or perhaps there's a better approach? Ideas and suggestions are welcome.

Replies are listed 'Best First'.
Re: mod_rewrite + CGI.pm in "reverse"?
by merlyn (Sage) on Mar 11, 2005 at 15:25 UTC
Re: mod_rewrite + CGI.pm in "reverse"?
by cleverett (Friar) on Mar 11, 2005 at 19:05 UTC
    I use mod_rewrite a a fair amount, and for something like this, I'd do:
    RewriteRule ^/(news|ftp|http)/([^/]+)/([\d]+)$ \ /index.pl?type=$1&feed=$1://$2&limit=$3

    I could be wrong about this, but the reason in my mind for using this little bit of voodoo is that .* will match from the second character to the end of the uri, leaving $2 and $3 undefined. ([^/]+)/([^/]+) is analogous to using (\w+)\W+(\w+) to pick out 2 series of non-spaces bracketing a length of whitespace.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://438673]
Approved by Arunbear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (6)
As of 2024-04-18 08:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found