dazzafact dazzafact - 1 month ago 16
Apache Configuration Question

htaccess match more then 10

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]

Answer

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
)
Comments