Oct 13, 2009 at 5:53 PM
Edited Oct 14, 2009 at 8:31 AM
you may be able to design a regex that matches the query string with the parameters in either order. But I think it would be hard to do, and it would be harder to understand than 2 distinct rules.
This regex comes pretty close: /prod.jsp\?.*?(cat|id)=([^=&]+).*?(cat|id)=([^=&]+).*?$
it matches these:
But it also matches this: /prod.jsp?id=3&id=1001
(In other words, a single param name, repeated).
This regex fixes that problem:
But it is getting pretty close to unreadable, and also the matched substrings are either $3 or $5, and $4 or $6, depending on which order cat and id are in. So this may be impractical. I think that's fixable with non-capturing groups but ... you know...
as obtuse as regex is, adding that twist would make it pretty much unmaintanable.
Better to have two rules, in my opinion.
RewriteRule /prod.jsp\?(.+?=.+?&)*cat=([^=&]+)(.+?=.+?)*&id=([^=&]+).*?$ /something-else/$2/$4 [I]
RewriteRule /prod.jsp\?(.+?=.+?&)*id=([^=&]+)(.+?=.+?)*&cat=([^=&]+).*?$ /something-else/$4/$2 [I]
This isn't exactly the easiest to read, but it's better than the more general, single regex. Before you use that, sure to test it all out, against expected and unexpected inputs.