0

This is a complicated one which I hope has a simple answer...

RewriteRule ^category/([^.]+)/([0-9]+)/([^.]+)/([0-9]+) category.php?c_id=$2&filters=$3&_p=$4&name=$1

This rule would pick up category/kitchen/10/0-0-0-0-0-0-0-0/1

with the following get vals:

category.php?c_id=10&filters=0-0-0-0-0-0-0-0&_p=1&name=kitchen

The reason filters were stored in 0-0-0-0-0-0-0-0 was because of the 9 back references limit. Each 0 was a different filter variable which I accessed by doing a split on $_GET['filter'].

I am now changing my URL to a non mod rewritten one, so that the rewrite rule becomes:

RewriteRule ^category/([^.]+)/([0-9]+)/([^.]+)/([0-9]+) category.php?c_id=$2&filters=$3&_p=$4&name=$1 [R=301,L]

Note to [R=301,L] so it becomes a 301 redirect.

This is all fine but I was wondering if there a was a clever way of splitting the 0-0-0-0-0-0-0-0 so that each 0 is a get variable. So I can get

category.php?c_id=10&f1=0&f2=0&f3=0&f4=0&f5=0&f6=0&f7=0&f8=0&_p=1&name=kitchen

Any idea?

Thanks in advance!

3
  • Minor nit: it's actually 10 back-references: $N where 0 <= N <=9.
    – earl
    Commented Sep 7, 2009 at 16:21
  • Apologies: # RewriteRule backreferences: These are backreferences of the form $N (0 <= N <= 9), which provide access to the grouped parts (in parentheses) of the pattern, from the RewriteRule which is subject to the current set of RewriteCond conditions.. # RewriteCond backreferences: These are backreferences of the form %N (1 <= N <= 9), which provide access to the grouped parts (again, in parentheses) of the pattern, from the last matched RewriteCond in the current set of conditions.
    – icelizard
    Commented Sep 7, 2009 at 16:29
  • To future viewers: This whole thing is just a pile of bad ideas. There are better ways to write web apps and the "problems" encountered here emphasize why this shouldn't be done.
    – Chris S
    Commented Sep 22, 2012 at 0:30

2 Answers 2

1

You could double the number of possible back-references by splitting the regex between a RewriteCond and a RewriteRule. Based on your examples, this would look something like the following:

RewriteCond %{PATH_INFO} ^category/[^.]+/[0-9]+/([0-9]+)-([0-9]+)-([0-9]+)-([0-9]+)-([0-9]+)-([0-9]+)-([0-9]+)-([0-9]+)/[0-9]+
RewriteRule ^category/([^.]+)/([0-9]+)/[^.]+/([0-9]+) category.php?c_id=$2&f1=%1&f2=%2&f3=%3&f4=%4&f5=%5&f6=%6&f7=%7&f8=%8&_p=$3&name=$1 [R=301,L]

Yes, I know, it's not the most elegant solution, but it should work. You may need to play a bit with the PATH_INFO (e.g. switching to REQUEST_URI or something similar) depending on your exact settings.

1
  • Thanks i took your idea and changed a littel: RewriteCond $3 ^([0-9]+)-([0-9]+)-([0-9]+)-([0-9]+)-([0-9]+)-([0-9]+)-([0-9]+)-([0-9]+)-([0-9]+)$ RewriteRule ^category/([^.]+)/([0-9]+)/([^.]+)/([0-9]+) category.php?c_id=$2&f1=%1&f2=%2&f3=%3&f4=%4&f5=%5&f6=%6&f7=%7&f8=%8&f9=%9&_p=$4&name=$1 [R=301,L]
    – icelizard
    Commented Sep 8, 2009 at 8:39
0

maybe you could use flags like 'next|N' (next round) after a RewriteRule to rerun the process with an useable break condition to avoid infinite loops

see here: http://askapache.info/trunk/mod/mod_rewrite.html#rewriterule

i haven't tried this yet but it may be possible

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .