optional unordered parameters

ISAPI_Rewrite is Apache mod_rewrite compatible URL rewriter for Microsoft IIS
User avatar
Posts: 5
Joined: 14 Jun 2012, 10:34

optional unordered parameters

14 Jun 2012, 10:41

hi, all,
my need is to rewrite the URL "/linea.asp?p=pppp&c=cccc&s=ssss&id=aaaa" to "/pppp/[/url]cccc/ssss/aaaa".
all 4 parameters have to be considered not compulsory and their order is not fixed.
can anybody please help me in defining one or more rules to achieve my goal?
thanks a lot!
d

User avatar
Posts: 5
Joined: 14 Jun 2012, 10:34

Re: optional unordered parameters

14 Jun 2012, 11:04

sorry: "/linea.asp?p=pppp&c=cccc&s=ssss&id=aaaa" should become "/pppp/cccc/ssss/aaaa".
d

User avatar
Posts: 1264
Joined: 07 Mar 2012, 10:16

Re: optional unordered parameters

14 Jun 2012, 11:13

Hello,

Well, there's no one magic-super rule for an issue like that... although we might come up with a set of rules.
So I'll give you an idea of what it could be for 1 parameter, then 2 parameters...

Code: Select all
RewriteEngine on
RewriteBase /

RewriteCond %{QUERY_STRING} ^p=([^&]+)$ [NC]
RewriteRule ^linea\.asp$ /%1 [NC,R=301,L]

RewriteCond %{QUERY_STRING} ^p=([^&]+)&c=([^&]+)$ [NC]
RewriteRule ^linea\.asp$ /%1/%2 [NC,R=301,L]


So depending on your URL architecture you may play a little with those rules and create some for 3 and 4 parameters. In case you need other combinations, create separate rule to foresee that situation.

While writing the above I've come up with another set of rules. The following must cover all combinations of 4 parameters:

Code: Select all
RewriteCond %{QUERY_STRING} p=([^&]+) [NC]
RewriteCond %{QUERY_STRING} c=([^&]+) [NC]
RewriteCond %{QUERY_STRING} s=([^&]+) [NC]
RewriteCond %{QUERY_STRING} a=([^&]+) [NC]
RewriteRule ^linea\.asp$ /%1/%2/%3/%4 [NC,R=301,L]


regards
Andrew

User avatar
Posts: 5
Joined: 14 Jun 2012, 10:34

Re: optional unordered parameters

14 Jun 2012, 12:32

could you provide the exact/complete rule?
sorry for my poor skill...
thanks a lot.
d

User avatar
Posts: 5
Joined: 14 Jun 2012, 10:34

Re: optional unordered parameters

14 Jun 2012, 12:47

I've setup the rule but, as you can see, http://www.mgkvis.it/linea.asp?id=MG.K- ... L-PRODOTTO works fine while http://www.mgkvis.it/MG.K-Vis-Istant-En ... L-PRODOTTO doesn't...
do You have an idea of where is the mistake?
d

User avatar
Posts: 5
Joined: 14 Jun 2012, 10:34

Re: optional unordered parameters

14 Jun 2012, 13:37

probably I didn't explain correctly:

"http://www.mydomain.com/linea.asp?p=pppp&c=cccc&s=ssss&id=aaaa" works fine

"http://www.mydomain.com/pppp/cccc/ssss/aaaa" I would like to write this seo friendly formatted URL, remembering that all parameters are optional and unordered.

thanks a lot.
d

User avatar
Posts: 1264
Joined: 07 Mar 2012, 10:16

Re: optional unordered parameters

15 Jun 2012, 06:58

Hello,

Firstly, to find a mistake we need to see how you adjusted the rule.

Secondly the rule I provided is for 301-redirect only. Creating of SEF-URLs usually consists of 2 parts: 301-redirect and rewrite. Redirect creates a pretty link in address bar and rewrite tells server what page we need to provide content from. This is a principle that I had to clarify before further explanation.

lets say you have couple pages that you can redirect using your scenario:

Code: Select all
linea.asp?id=MG.K-Vis-Istant-Energy&s=PLUS-DEL-PRODOTTO  ---> 301-redirect ---> MG.K-Vis-Istant-Energy/PLUS-DEL-PRODOTTO
linea.asp?id=MG.K-Vis-Istant-Energy&d=XYZ&s=PLUS-DEL-PRODOTTO ---> 301-redirect ---> MG.K-Vis-Istant-Energy/XYZ/PLUS-DEL-PRODOTTO
linea.asp?s=PLUS-DEL-PRODOTTO ---> 301-redirect ---> /PLUS-DEL-PRODOTTO


My point is that at this stage(after 301-redirect) there's no way for the server to know which values belong to 's', 'id' and 'd'. So we can only redirect, but there's no way to rewrite random number of parameters that are might have random order.

The solution - to change the scenario, like:

/linea.asp?id=XXXXX&c=YYYYY&d=ZZZZZZ should become id/XXXX/c/YYYYY/d/ZZZZZ
/linea.asp?d=ZZZZZZ&c=YYYYY should become /d/ZZZZZ/c/YYYYY


So parameters and values are staying together and we'd be able to write a rewrite rule.

Regards
Andrew

Return to ISAPI_Rewrite 3.0

Who is online

Users browsing this forum: No registered users and 6 guests