Forum Moderators: phranque

Message Too Old, No Replies

mod_rewrite - replace parameter seperators in URI

         

Cronjob

10:50 am on Apr 12, 2004 (gmt 0)

10+ Year Member



I've searched everywhere for an example of what I'm trying to do, with no success.

My Perl software expects requests like this:

[example.net...]

The requests I'd like users to submit would be like this:

[example.net...]

The problem is that the same parameters may not alwaysbe used or used in the same order. Sometimes, they may not even be present at all. So how can I dynamically handle this?

Even if I could figure out how to turn every occurance of '_~' into '=', I would still need to figure out how (and when) to start the parameter list by placing the '?' in the right place and when to substitute '/' with '&'.

This is what epinions.com seems to do and I'd like to accomplish the same thing, if possible (they're running apache):

[epinions.com...]

Any clues or guidance? I could really use it. I've been at this forever. Or should I just give up and use the standard?, & and =?

jdMorgan

10:17 pm on Apr 12, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Cronjob,

While it's our policy as stated in our charter [webmasterworld.com] to ask you to write your own code and post it here first, this is a fairly interesting & novel problem, so this thread may help some other members in the future. The following code will "get you close" to what you need to do, but you'll probably have to do some work on it anyway.

I have intentionally omitted the "~" characters from your "input" URIs, because they're not necessary, and because short, easy-to-type URIs are better. Feel free to put them back if you insist. :)

The following code takes advantage of the [QSA] (query string append) flag to successively build-up the query string in several rewrite steps. Any parameter not yet transferred into the query string is retained until the very last step, and the method shown makes the order of output parameters independent on their order or presence in the input URI.

Rewrite /forums/f124/sortby_date/sortdir_asc/page_3 -> /bar.cgi?c=forums&forum=124&page=3&sortby=date&sortdir=asc


Options +FollowSymLinks
RewriteEngine on
RewriteRule ^/forums(.*)/sortby_([^/]+)(.*)$ /forums$1$3?sortby=$2
RewriteRule ^/forums(.*)/sortdir_([^/]+)(.*)$ /forums$1$3?sortdir=$2 [QSA]
RewriteRule ^/forums(.*)/page_([0-9]+)(.*)$ /forums$1$3?page=$2 [QSA]
RewriteRule ^/forums(.*)/f([0-9]+)(.*)$ /forums?c=forums&forum=$2 [QSA]
RewriteRule ^/forums /bar.cgi [L]

A few notes: First, the above code is intended for use in httpf.conf. If you want to use it in .htaccess, most of the leading slashes will have to be removed:

Options +FollowSymLinks
RewriteEngine on
RewriteRule ^forums(.*)/sortby_([^/]+)(.*)$ forums$1$3?sortby=$2
RewriteRule ^forums(.*)/sortdir_([^/]+)(.*)$ forums$1$3?sortdir=$2 [QSA]
RewriteRule ^forums(.*)/page_([0-9]+)(.*)$ forums$1$3?page=$2 [QSA]
RewriteRule ^forums(.*)/f([0-9]+)(.*)$ forums?c=forums&forum=\1$2 [QSA]
RewriteRule ^forums /bar.cgi [L]

The Options directive may or may not be needed on your server as currently configured. If it is not needed, it is possible that including it may cause server errors. Remove it if you need to.

The RewriteEngine on directive is only needed once, unless you are turning the engine on and off for debugging purposes.

Posting on this forum modifies the pipe "¦" character from a solid pipe to a broken pipe. You will have to edit the code above to replace the broken pipes with solid pipes before use.

This code generates a server-internal rewrite only. The new URI *will not* be visible to the user's browser. However, the internal URI will be /bar.cgi, and the server environment variable QUERY_STRING will contain the parameters in the order you specified.

Lastly, the parameters are processed in reverse order, but will end up in the order you specified due to the action of the [QSA] function.

Ref:
Introduction to mod_rewrite [webmasterworld.com]
[httpd.apache.org...]

Jim

Cronjob

4:11 am on Apr 13, 2004 (gmt 0)

10+ Year Member



Thank you - this is exactly what I was looking for.

I would have posted my own code, except I had no code (related to this specific rewrite) available since I didn't even have a place to start.

I kept thinking there had to be a way I could, in one simple line, do an accross the board s/~_/=/, but feared it would requirte multiple rewrites or rewriteconds like in your example.

Not that such a solution isn't reasonable - but I'm a coder and only an apache admin by necessity so I have no idea how these things are handled in real-life corporate type situations.

Is there a repository of mod_rewrite example rules somewhere that I haven't stumbled upon? Of course, I've reviewed everything related to mod_rewrite on apache.org (I've been administering my own server which has approximately 50,000 regular members and a few million visitors a month - it's a free nich auction site I've run as a hobby since 1999) - but the best I've come upon is a handful of simple examples here and there through usenet and forums like this one.

I suppose what I'm specifically looking for is some sort of mod_rewrite cookbook.

Anyway - thank you very much for your kind assistance. As someone who has to deal with stupid questions every day, I hate asking for help and only do so when I've come to my wits end... and this was one of those dire situations where I was completely frustrated and baffled. :)

jdMorgan

4:47 am on Apr 13, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I don't know of a cookbook, except for the URL Rewriting Guide on the Apache site. I sure wish there was a good one, though; I had to just muddle through, myself, collecting several hundred 500-Server Errors in the process. :)

I think that the main problem is that mod_rewrite is basically a compact programming language, and as such, there is no one-size-fits-all solution or building-block method available. Except for the very basics like redirecting subdomains to the main domain, every problem and solution is different - Try searching this site for "RewriteRule" and see - 2,850 posts!

Jim