Forum Moderators: phranque

Message Too Old, No Replies

Grouping in Apache's ModRewrite

Group without creating a substring?

         

rob7591

7:37 pm on May 24, 2008 (gmt 0)

10+ Year Member



Hi,

I was wondering if there's a way to group characters without using them as a substring (variable thing) for ModRewrite Regexp's.

For example, I want to group:

(.+)(-mytext/)? but I don't want -mytext/ to be $2, I just want to have the group.

Hope that explained it easy enough.

rob7591

8:24 pm on May 24, 2008 (gmt 0)

10+ Year Member



Is there a way to edit posts? :\

It's better understood if you look at it like this:

((.+)-mytext/)? and I want the (.+) to be $1, but only return it if it's followed by mytext, so if it said: hi-mytext ,$1 would be hi.
if it said hi-yourtext, $1 would be blank

rob7591

11:09 pm on May 24, 2008 (gmt 0)

10+ Year Member



Sorry I keep replying to myself, but I'm getting closer :o

I found a preg way to do what I want, but I don't think Apache supports preg, does it? Anyway:

/(?:(.+)-mytext)?/

(note ?: creates a non-backreferencing group, (that's the vocab word I'm looking for :} )

jdMorgan

11:23 pm on May 24, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I don't know of any way to make mod_rewrite NOT create a back-reference if a sub-pattern is parenthesized.

Jim

rob7591

1:57 pm on May 25, 2008 (gmt 0)

10+ Year Member



Ok, is there a way to group text without using parenthesis then?

jdMorgan

2:17 pm on May 25, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



No. Step back and re-assess. There is likely another way to do what you want to do. If there is no other better way, you can use a RewriteCond to "move" the value from %2 to %1 -- ugly, but it works.

Jim

rob7591

4:26 pm on May 25, 2008 (gmt 0)

10+ Year Member



Well here's my situation,

I have 6 "properties" for my widgets that I want users to be able to narrow down by: company, price range, type, free shipping, rebates, (and one other for the ORDER BY part).

I want the user to be able to select any combination of these to narrow it down, so I had this:
RewriteRule ^sub/((.+)-c/)?((.+)-t/)?((.+)-p/)?((.+)-r/)?((.+)-f/)?((.+)-o/)? subindex.php?company=$2&type=$4&price=$6&rebates=$8&freeshipping=$10&order=$12 [L]

But of course you can't have over $9, so the easiest way would be to make the container groups: (..-c/) non-backreferencing so I would only have $1 - $6, but as we've established, that doesn't work.

So now I'm trying to figure out the whole $9 [QSA] stuff, unless you see a better way?

Thanks so far

jdMorgan

5:07 pm on May 25, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well, let's by-pass the whole problem then, and put the vars into server variables for later re-assembly:

RewriteRule /([^/\-]+)-c/ - [E=company:$1]
RewriteRule /([^/\-]+)-t/ - [E=type:$1]
RewriteRule /([^/\-]+)-p/ - [E=price:$1]
RewriteRule /([^/\-]+)-r/ - [E=rebates:$1]
RewriteRule /([^/\-]+)-f/ - [E=freeship:$1]
RewriteRule /([^/\-]+)-o/ - [E=order:$1]
RewriteRule /sub/.+ subindex.php?company=%{ENV:company}&type=%{ENV:type}&price=%{ENV:price}&rebates=%{ENV:rebates}&freeshipping=%{ENV:freeship}&order=%{ENV:order} [L]

Jim

rob7591

5:30 pm on May 25, 2008 (gmt 0)

10+ Year Member



You're the best,

Thank you so much, I didn't even know that was possible.

jdMorgan

6:10 pm on May 25, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This technique is even more useful when applied to query strings, where it's the only fully-capable way of extracting large numbers of name/value pairs when they can occur in any order.

Jim