【问题标题】:htaccess match more then 10htaccess 匹配超过 10 个
【发布时间】:2016-10-16 11:43:17
【问题描述】:

您好,我试图在我的 htaccess 中匹配超过 10 个变量,但在第 10 个变量之后它无法识别并以第一个 $1 开头 - 但我希望“$11”不是 $1

浏览器网址:http://localhost/s/adobe+cs5/58058,18793/111/666/

htaccess 重写: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]

【问题讨论】:

    标签: regex apache .htaccess mod-rewrite url-rewriting


    【解决方案1】:

    您的正则表达式中有很多不必要的可选捕获组。您可以将“可选组”转换为非捕获组,即(?:...),以使捕获组的数量少于 10。

    你可以使用这条规则:

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

    这将处理这个 URL:

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

    search.php 中填充这些GET 参数:

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

    【讨论】:

    • 好的,但是如果匹配都是必要的呢?您如何参考 9 号之后的比赛?在 JS 中这似乎可行,但在 .htaccess $10 及以后的版本中会产生 $1 + 0 的串联值。
    • 为了捕获超过 9 个值,我们必须将规则分成两部分,因为我们不能超过 9 美元
    • 啊哈有趣。谢谢。
    猜你喜欢
    • 1970-01-01
    • 2013-07-29
    • 2023-01-01
    • 2012-06-12
    • 2010-12-12
    • 2014-09-13
    • 2011-08-02
    • 2019-02-17
    • 1970-01-01
    相关资源
    最近更新 更多