mmundiff
mmundiff

Reputation: 3951

php mod rewrite

i have a rule which:

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

goes to cat_ap.php?nid=xxxxxxx

now I have to add some other support.

cat_ap~pnid~290~nid~96666~posters~Sports.htm

needs to resolve to cat_ap.php?pnid=290&nid=96666 (I'm not concerned with the end of the link though I do need it in there for legacy paid search traffic).

Also, I need to get a page_num out of the link to calculate pagination.

cat_ap~PageNum_GetProduct~10~nid~290.htm needs to resolve to cat_ap.php?PageNum_GetProduct=10&nid=290

I kind of need to encompass all these scenarios either into a single or multiple rules.

Any ideas? I'm reading up on this right now but am stuck.

Thanks

Upvotes: 0

Views: 408

Answers (3)

Ben Blank
Ben Blank

Reputation: 56654

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.)

Upvotes: 1

Alekc
Alekc

Reputation: 4770

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.

Upvotes: 1

J.C. Inacio
J.C. Inacio

Reputation: 4472

Try this for both cases:

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

Upvotes: 0

Related Questions