2

Hello iam trying to match more then 10 variables in my htaccess , but after the 10th variable it doesnt recognize and begin with the first $1 - but i expect "$11" not $1

Browser Url: http://localhost/s/adobe+cs5/58058,18793/111/666/

htaccess Rewrite: Array ( [search] => adobe cs5 [categories] => Array ( [0] => 58058 [1] => 18793 [2] => ) [pricefrom] => 111 [priceto] => adobe cs51 [email] => adobe cs53 )

RewriteEngine On
RewriteBase /eule2/
RewriteRule ^s/([^/\.]+)/(([0-9]+)(,([0-9]+?))?(,([0-9]+?))?/)(([^/]+)/)?(([^/]+)/)?(([^/]+)/)?((?<email>[^/]+)/)?$ 
search.php?search=$1&categories[]=$3&categories[]=$5&categories[]=$7&pricefrom=$9&priceto=$11&email=$13 [L]
anubhava
  • 761,203
  • 64
  • 569
  • 643
dazzafact
  • 2,570
  • 3
  • 30
  • 49

1 Answers1

1

You have quite a few unnecessary optional capturing groups in your regex. You can convert "optional groups" to non-capturing groups i.e. (?:...) to bring count of capturing groups to less than 10.

You can use this rule:

RewriteRule ^s/([^/.]+)/(\d+)(?:,(\d+?))?(?:,(\d+?))?/(?:([^/]+)/)?(?:([^/]+)/)?(?:([^/]+)/)?(?:([^/]+)/)?(?:([^/]+)/)?$ search.php?search=$1&categories[]=$2&categories[]=$3&categories[]=$4&pricefrom=$5&priceto=$6&email=$7 [L,QSA,NC]

This will handle this URL:

http://localhost/s/adobe+cs5/58058,18793/111/666/adobe+cs53/

to populate these GET parameters in search.php:

Array
(
    [search] => adobe cs5
    [categories] => Array
        (
            [0] => 58058
            [1] => 18793
            [2] => 
        )
    [pricefrom] => 111
    [priceto] => 666
    [email] => adobe cs53
)
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • OK sure, but what if the matches *were* all necessary? How do you reference a match after the 9th? In JS this seems to work, but in .htaccess `$10` and onwards result in the concatenated value of `$1` + `0`. – Mitya Nov 10 '19 at 19:18
  • For capturing more than 9 values we will have to break out rule in 2 parts since we cannot anything above $9 – anubhava Nov 11 '19 at 03:48
  • 1
    Aha interesting. Thanks. – Mitya Nov 11 '19 at 10:22