Forum Moderators: phranque

Message Too Old, No Replies

merging parameters in mod rewrite

merging parameters in mod rewrite

         

fahad direct

7:12 am on Apr 15, 2010 (gmt 0)

10+ Year Member



Hi,I have a link say:

http://www.example.com/1021131

here if you see the numberr 1021131 , this number is actually a combination of 3 ids being passed together like
id1= its range can be 1 to 3 digits
id2= its range can be 1 to 2 digits
id3= its range can be 1 to 2 digits

but i need to write them all together and not like
http://www.example.com/102/11/31

so how can i make a pattern for it where i don't know is only 1 or first 2 or first 3 digits attached with id1?

[edited by: bill at 7:22 am (utc) on Apr 15, 2010]
[edit reason] Use example.com for examples. It can never be owned. [/edit]

g1smd

7:36 am on Apr 15, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



If this were my URLs, and assuming ID 2 and 3 are always two digits, I would make the variable-length number the last item, not the first. The pattern is then
([0-9]{2})([0-9]{2})([0-9]{1,3})
and can be read in one pass (for ID 3, 2, 1 in that order).

However, going with your way for a moment, if the second and third ID are always two digits, you can use
([0-9]{1,3})([0-9]{2})([0-9]{2})
or something similar. If the other parts are also variable in length this is unsolvable without adding separators.

The preceding pattern isn't very efficient, needing several back off and retry attempts before a match might be found. The alternative is to test the number length first and have three separate rules to split it up.

First rule tests
^[0-9]{7}$
and then
([0-9]{3})([0-9]{2})([0-9]{2})
in a RewriteCond.

Second rule tests
^[0-9]{6}$
and then
([0-9]{2})([0-9]{2})([0-9]{2})
in a RewriteCond.

Third rule tests
^[0-9]{5}$
and then
([0-9]{1})([0-9]{2})([0-9]{2})
in a RewriteCond.

The RewriteCond will generate the %1, %2 and %3 backreferences for later use.

The above assumes that ID 2 and 3 are always two digits. If those IDs are also variable in length this is unsolvable without adding separators, or without using a leading zero.

My way to solve this would be to make sure that all references are exactly seven digits, using leading zeroes where appropriate.

The pattern is then simply
^([0-9]{3})([0-9]{2})([0-9]{2})$


At present it is impossible to tell whether 123456 is 123, 4, 56 or is 123, 45, 6 or is 12, 34, 56. I would use 1230456 and 1234506 and 0123456 to be absolutely sure.

fahad direct

8:03 am on Apr 15, 2010 (gmt 0)

10+ Year Member



Excellent , thanks for your very detailed help :-)

g1smd

7:05 pm on Apr 15, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I've just done something like this where,, for example, the first four digits are the maker and the last five digits are the item number (or something like that).

To try to avoid transcription mistakes we didn't start at
000100001
though.