Need help - writing a rule

Topics: User Forum
Jun 20, 2008 at 12:23 PM
Can You help me write a rule for processing something like this:

www.mydomain.com/item15-lcd-samsung-le-46m86bd.html
to
www.mydomain.com/item.aspx?id=15

www.mydomain.com/item16-sony-bravia-kdl-40w4000.html
to
www.mydomain.com/item.aspx?id=16

Many thanks,
Sebastian
Coordinator
Jun 20, 2008 at 6:22 PM

how about this:

RewriteRule ^/item(\d+)-([^\.]+)\.html$  /item.aspx?id=$1 [L]

To explain: The caret is the beginning-of-line character. Then a slash - all URLs begin with a slash. The (\d+) says, any series of 1 or more successive digits. (\d indicates [0-9]) Then there is a dash, followed by any series of characters not including a dot (period). Then there is the dot followed by html, keep in mind that the dot itself is meaningful in the Regular Expression language - it is a wildcard character. So we have to escape it with a backslash. Then the line ends, so we use the end-of-line indicator ($).  That's it for the Match pattern. 

The replacement string is just what you asked for. The $1 indicates the first matched substring, which in our case is the series of digits.

A reminder - this rule works for the cases you gave me. You will want to test other cases. Also you may want other rules to handle "broken" or badly formed URLs as well. You might want to handle just www.mydomain.com/item16 too.  And so on. 

ps: just a gentle reminder: IIRF is donationware - click here for more info.  If you find IIRF useful, consider donating.