Forum Moderators: phranque

Message Too Old, No Replies

A bit of mod rewrite help if possible.

Rewrite rules help

         

Munkii

11:52 pm on Oct 21, 2009 (gmt 0)

10+ Year Member



I'm new to mod_rewrite, and I've been tinkering with a few ideas lately.. This one however, I'm unable to figure out on my own, and I'd really like to get this working..

I think it's an easy fix.. but then again like I said, I'm new to mod_rewrite and don't really know the best way to approach things.

-----------------------------------------

What I'm trying to do is rewrite:

video.php?do=viewcategory&categorytitle=XX&categoryid=XX

to

video/category/XX-XX

An example would be:

video.php?do=viewcategory&categorytitle=after-school&categoryid=3

to

video/category/after-school-3

The rule I have is:

RewriteRule ^video/category/([a-z0-9_\-]*)([0-9])$ video.php?do=viewcategory&categorytitle=$1&categoryid=$2&sort=dateline&order=ASC [L]

The Problem:

It seems to work for the category IDs 1 through 9.. Anything higher than 9 though, as in 2 or 3 digit IDs, don't work.. And instead the last digit is used as the category ID..

So category 33 would come out as category 3..

I'm really not sure how to fix this, and I don't think it's a hard fix.. so any help would be greatly appreciated.

Thanks,
Peter (Munkii)

jdMorgan

12:14 am on Oct 22, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Just to be clear, you are in fact rewriting the 'friendly' requested URL to the script filepath, not the other way around... This is an internal URL-to-filepath translation rewrite.

You need to add a quantifier to make subpatterns such as "[0-9]+". Use "+" to match one or more digits, or specify a minumum,maximum range by using an explicit quantifier range, for example "{2,9}". You may also omit either the minimum or the maximum if you wish.

You may also use a quantifier of "*" which will match zero or more characters. As you have found though, it causes problems because it is maximally-greedy, and will consume *all* characters in the string to be matched if you allow it to do so, leaving the next sub-pattern to "starve." I suspect you won't be able to allow numbers in the 'title' if you want to use a simple pattern for the title. Otherwise, you'll need something more complex like:

^video/category/(([a-z0-9_]+-)+)([0-9]+)$

and you'll need to back-reference $1 and $3 in the substitution.

See the concise regular-expressions tutorial cited in our Forum Charter for details.

Jim