Dynamic url parameters

ISAPI_Rewrite is Apache mod_rewrite compatible URL rewriter for Microsoft IIS
User avatar
Posts: 7
Joined: 16 Jul 2012, 07:59

Dynamic url parameters

21 Apr 2014, 12:01

I've tried to find this solution already but couldnt so I hope someone can help.

Im looking to add search parameters to a url, I can do what I want but if a parameter is missing it all breaks, I think I know the cause but I dont know the solution. Essentially there could be any number of parameters from 1 to 8. I dont want to fix it to always have the max amount so I need the rule to be flexible. To show an example of what I mean, on another site I have :

RewriteRule ^(\w+)-(\w+)-for-sale-([a-z]*)([0-9]*)([a-z]*)([0-9]*)([a-z]*)([0-9]*)([a-z]*)([0-9]*)([a-z]*)([0-9]*)([a-z]*)([0-9]*)([a-z]*)([0-9]*)([a-z]*)([0-9]*)([a-z]*)([0-9]*)([a-z]*)([0-9]*) /index.asp?r=1&place=$1&typ=$2&$3=$4&$5=$6&$7=$8&$9=$$10&$$11=$$12&$$13=$$14&$$15=$$16&$$17=$$18&$$19=$$20&$$21=$$22&$$23=$$24 [NC,L]

This works with this url :
domain.com/blah-blah-for-sale-ppp12pg1bed3bth3loc2311twn2356min50000max200000pt1
but it would also work with this :
domain.com/blah-blah-for-sale-ppp12pg1bed3bth3

now the new url I want isnt a mix of numbers and letters, sadly, but its very close i.e this is a sample
domain.com/blah-blah-for-sale-p1o10bed0pmin20000pmax100000locMalagatwnAlcaucintyApartment

As you can see it starts off with a mix of [letter][number] etc but then changes to [word][word], broken down it should read :
p=1
o1=0
bed=0
pmin=20000
pmax=100000
loc=Malaga
twn=Alcaucin
ty=Apartment

The rule I have that works is :
RewriteRule ^(\w+)-(\w+)-for-sale-([a-z]*)([0-9]*)([a-z]*)([0-9]*)([a-z]*)([0-9]*)([a-z]*)([0-9]*)([a-z]*)([0-9]*)(loc)([a-z]*)(twn)([a-z]*)(ty)([a-z]*) /index.asp?r=1&$3=$4&$5=$6&$7=$8&$9=$10&$11=$12 [NC]
url to try :
blah-blah-for-sale-p1o10bed0pmin20000pmax100000locMalagatwnAlcaucintyApartment

but the problem is that if 1 parameter is removed, the rule isnt matched. Unlike the first rule, which will match no matter the number of parameters included. I think this is because its looking for "(loc)" etc and if that isnt included, it fails. So I need to make those optional i.e. match even if its not found.


Alternatively if anyone has another solution for a dynamic number of parameters in a url, Im all ears as this is frustrating me. I did look at this http://www.helicontech.com/isapi_rewrit ... es.htm#SEF but I cant see how this would possibly work for me.


note : I have been using the regexTest program to try to find my solution. My first working rule works on a live site, its the 2nd one I cant get to work. Hopefully someone can help

edit
my version of regextester is very old now so if theres a download section for the most current one I would be grateful for a link

User avatar
Posts: 7
Joined: 16 Jul 2012, 07:59

Re: Dynamic url parameters

21 Apr 2014, 12:21

Update
I've had a further play using regextest, and the sample here http://www.helicontech.com/isapi_rewrit ... es.htm#SEF

using rule - RewriteRule ^(blah.asp)/([^/]*)/([^/]*)(/.+)? $1$4?$2=$3 [NC,LP]
using data - blah.asp/a/1/b/2/c/3
resulting url - blah.asp?c=3?a=1?b=2
which is almost what I want but you notice all the extra "?" in there, should just be 1, and the rest should be & but I cant figure out how to get it to work as any changes break the rule. While this isnt ideal to my situation I could use this instead of what I really want, if what I really want is impossible.

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

Re: Dynamic url parameters

21 Apr 2014, 23:37

Hello,

The '?' will save you:

Code: Select all
RewriteRule ^(blah.asp)/([^/]*)/([^/]*)(/.+)? $1$4?$2=$3? [NC,LP]


It prevents appending querystring at the end.

Regards

Return to ISAPI_Rewrite 3.0

Who is online

Users browsing this forum: No registered users and 0 guests