views:

325

answers:

3
A: 

Try this for both cases:

RewriteRule ^cat_ap~([^~]+)~([0-9]+)~([^~]+)~([0-9]+)(.*)\.htm$ /urban/cat_ap.php?$1=$2&$3=$4 [L]
jcinacio
+1  A: 

In this kind of scenario i'd suggest to use the rule RewriteRule ^cat_ap(~.*).htm$ /urban/cat_ap.php?$1 [L] in order to catch all possible combination and decompose the string inside your php application.

In this way it will be easier to debug parameters, since apache can be pretty bastard sometimes.

Alekc
+1  A: 

Here's a solution which will loop over key/value pairs in your URL:

RewriteRule ^cat_ap~([^~]+)~([^~]+)(.*)\.htm$ cat_ap$1?$1=$2 [N,QSA]
RewriteRule ^cat_ap(.*)\.htm$ /urban/cat_ap.php [L]

It grabs the first pair off the URL, then rewrites it to the exact same URL without that pair, adds the pair to the query string ([QSA]) and starts the rewriting process over again ([N]). When there are no more key/value pairs in the URL, it rewrites it to your script's location and terminates rewriting ([L]).

cat_ap~pnid~290~nid~96666~posters~Sports.htm => /urban/cat_ap.php?posters=Sports&nid=96666&pnid=290
cat_ap~PageNum_GetProduct~10~nid~290.htm => /urban/cat_ap.php?nid=290&PageNum_GetProduct=10

(Note that the order of parameters is reversed; this will only matter if the same parameter appears more than once.)

Ben Blank
Actually, [R] sends a 301 redirect back to the browser, which could cause a long (and unnecessary) chain of HTTP redirects if there are many parameters. I'd suggest using [N] instead (in context: [N,QSA]).
David Zaslavsky
Thanks, that's what I meant. >.<
Ben Blank