Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mod_rewrite number of parameters/back-references limitation

Apparently there is a limitation (9) on how many backreferences you can access in htaccess RewriteRules..

But we have a RewriteRule that requires more than 9 parameters, something like this:

RewriteRule ^([^/]+)/b([0-9]+)(/a([0-9]+))?(/v([0-9]+))?(,([0-9]+))?(/(ajax|share))?(,complete)?$ /index.php?control=sites&site=brands&control_file=version_select&name=$1&campaign_id=$2&answer=$4&page=$8&option=$10&video_id=$6&page_type=research [L]

So what happens when you try to access "$10", is that it uses $1 and puts a 0 right next to it.

Has anybody been able to solve this problem, any alternative solutions or anything?

Thanks!

like image 424
adamJLev Avatar asked Apr 03 '09 18:04

adamJLev


3 Answers

Actually, you don't need to capture everything. Write non-capturing groups (introduced with "?:") for the things you don't want to reuse, this should give you some breathing space again. Compare:

Yours:                       Mine:
-------------------------    ---------------------------
^([^/]+)/b          $1       ^([^/]+)/b            $1
([0-9]+)            $2       ([0-9]+)              $2
(/a([0-9]+))?       $4       (?:/a([0-9]+))?       $3
(/v([0-9]+))?       $6       (?:/v([0-9]+))?       $4
(,([0-9]+))?        $8       (?:,([0-9]+))?        $5
(/(ajax|share))?    $10!     (?:/(ajax|share))?    $6
(,complete)?$       $11!     (,complete)?$         $7

But with mod_rewrite alone, you can't go higher than 9 back-references. If you need more, use an alternative - for example capturing only the most important parts in rewrite and do some string-processing with the rest of the URL in your app.

like image 159
Tomalak Avatar answered Nov 01 '22 04:11

Tomalak


One, although pretty dumb, idea would be to do the rewrite in two steps, that is, chain two RewriteRules with the first rule rewriting it into some intermediate format.

like image 44
andri Avatar answered Nov 01 '22 03:11

andri


One solution might be to use the same regex, without captures, and pass the request to index.php

Then use index.php to explode the Request URI by the forward slashes and you can work from there with the data.

like image 21
David Snabel-Caunt Avatar answered Nov 01 '22 03:11

David Snabel-Caunt